Create a jar that contain dependency management maven [duplicate]

隐身守侯 提交于 2019-12-13 10:21:15

问题


I already create a jar that contain some common jars, which we will use it in different applications and can be used via many users.

To Solve this, i create a common jars, so i can control this commons jars and version of them, then store it in nexus server, so it can contain the parent pom and can be used by many developers.

So i create a simple maven java application and in the pom of this jar, i put for example :

Parent pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.emp</groupId>
    <artifactId>dependencymanager</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
                <version>6.0</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.1.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

Then in the child applications i use this jar like this:

<dependencies>
    <dependency>
        <groupId>com.emp</groupId>
        <artifactId>dependencymanager</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

But when i try to use org.primefaces, i can't. Where i'm wrong, or i'm wrong in the implementation of dependency management?

Note

I already read this : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

So in the example of this tutorial it not specify the version of the parent pom, so when i delete it show me an error like this :

'dependencies.dependency.version' for com.emp:dependencymanager:jar is missing.

Question

  • How can i implement dependency management in this case?
  • Is there any better way then this to work with same dependencies with many projects and many devellopers.
  • I use nexus to store my jars

Thank you.


回答1:


In the parent pom, add the dependencies directly, leaving out the dependencyManagement tags. With the dependencyManagement you only specify that if such a dependency is declared in your pom, that the version specified in the parent pom is to be used. But your parent pom does not per se include those dependencies as transitive dependencies.




回答2:


I have used Maven Shade plugin for having dependencies in the generated jar file. Check if similar code works for you

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>


来源:https://stackoverflow.com/questions/41919702/create-a-jar-that-contain-dependency-management-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!