4

I have the following structure:

main/
  --pom.xml
  --common/
      --pom.xml
  --core/
      --pom.xml

Both common and core are modules of the main projects, so main/pom.xml is like this:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>main</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Main APP</name>
  <url>http://maven.apache.org</url>
  <modules>
      <module>common</module>
      <module>core</module>
  </modules>
</project>

Besides that, common/pom.xml is supposed to hold some common dependencies between all future modules. This is how common/pom.xml looks like for now:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>common</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>mod1</name>
    <url>http://maven.apache.org</url>


    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>main</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>common-dependency</groupId>
                <artifactId>common</artifactId>
                <version>3.8.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Finally, the module core should use dependencies defined in the common module, so this is what I done in core/pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Core</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>main</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.mycompany.app</groupId>
                <artifactId>common</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

What I wanted is that when I run mvn dependency:copy-dependencies inside core module, maven should copy all the dependencies from common module, also (that common-dependency:common, for example). But actually what happens is that it do not copy any dependency, so it's like if core module didn't had any dependency at all.

What am I missing here?

Chico Sokol
  • 1,254
  • 3
  • 16
  • 24

1 Answers1

1

Scope 'import' is for dependencyManagement, that only gives you the versions and default scopes of modules, when you actually use them.

If you want to get the dependencies, you don't need to wrap them in dependencyManagement, and just use transitive dependency mechanism.

So common/pom.xml should be

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>common</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>mod1</name>
    <url>http://maven.apache.org</url>


    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>main</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

        <dependencies>
            <dependency>
                <groupId>common-dependency</groupId>
                <artifactId>common</artifactId>
                <version>3.8.1</version>
            </dependency>
        </dependencies>
</project>

And core/pom.xml should be

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Core</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>main</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

        <dependencies>
            <dependency>
                <groupId>com.mycompany.app</groupId>
                <artifactId>common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>

</project>
GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • I did not work, when I run `mvn package` in the core module I get the following error: `...Could not find artifact com.mycompany.app:common:jar...` – Chico Sokol Sep 07 '13 at 12:54
  • You need to do 'mvn install' on the common jar first. Or do 'mvn install' in the parent folder, and maven will figure out the correct order. 'mvn package' will only build a jar in the local module's target directory, you need 'mvn install' to put it in the local repository, so it's available for other modules, including sibling modules. – GeertPt Sep 09 '13 at 10:34