-1

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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

2 Answers2

1

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>
mc20
  • 1,145
  • 1
  • 10
  • 26
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.

cello
  • 5,356
  • 3
  • 23
  • 28
  • thank you @cello when i remove de `dependencyManagement` it work fine, this part is solved, so what if i change the version of `primefaces` for example, then store this jar in nexus can the version changed for all the users? – Youcef LAIDANI Jan 29 '17 at 10:33
  • as long as the users don't add primefaces on their own with another version, then the version specified in the parent pom will be used. – cello Jan 29 '17 at 10:34
  • so why people talk about `dependencyManagement` how, where and when i can use it? – Youcef LAIDANI Jan 29 '17 at 10:35
  • The dependencyManagement section is useful to specify the version of dependencies, *when* they are used. In the "child" pom, one could then just add the group and artifact id of a dependency, leaving out the version. The version would then be taken from the parent pom's dependencyManagement section. – cello Jan 29 '17 at 10:36
  • so this is what i want to do, for that i create this jar with `dependencyManagement section` to use this version and not another one, but i think this not work, i'm sorry for my questions – Youcef LAIDANI Jan 29 '17 at 10:39
  • so if this not work, can you propose to me another architecture that solve my issue ? – Youcef LAIDANI Jan 29 '17 at 10:40
  • The question is: do you want the dependencies to be automatically available in your project or not. If yes: do it without the dependencyManagement. If not, and you only want to specify the versions to be used in case somebody wants to use it, then do it with dependencyManagement. – cello Jan 29 '17 at 10:48
  • ok, here is this scenario maybe i could not explain it good : consider i create this parent jar and put it in nexus, so users use this jar to get dependencies of primefaces, like we use now, so what if i change the version of primefaces, this not change in the other the version of the others users, unless they don't remove this jar from the local repository or change the version of the parent jar, is that true @cello ? – Youcef LAIDANI Jan 29 '17 at 10:59