简体   繁体   中英

Another own Maven project as dependency - how to solve it?

I have three Java console applications - A, B and C, all are managed with Maven. Application A contains common tools classes and these tools are used by another two applications - B and C. I need to know how to solve this dependency with Maven.

I have an idea - please correct me if I am wrong, to package and install application A into local Maven repository via mvn install command and this library set as new dependency for app B and C in their pom.xml files.

Is this a good idea or is there some better way how to solve this issue?

This question seems to be related to my issue, but I'm not able to judge if is it a good solution in my situation - How do I add a project as a dependency of another project?

Yes, it's perfectly fine. This solution also scales - you may later install you artifacts to remote repositories or even Maven Central.

Some other option is to place these three projects as modules under a common parent POM with packaging set to pom . The advantage is that you would be able to install all three artifacts with a single command and ie update dependencies on-the-fly when using Maven Release plugin.

Yes, this seems like the right way to solve this problem. Alternatively you can deploy to a real repository. That would be even better if you want other people to build project B and C without installing A first.

I would put the code that B and C use in its own separate jar. That way B and C don't use all of project A, but just a library with the code that they need.

I would not use the advice of the linked article. It uses a system path which is not ideal because it needs an absolute path so it will only work if someone installs the projects in the same place on their file system as you.

Running mvn install will solve your problem, but then you are solving it manually. It creates many complications when your projects change. What you need is a pom.xml that makes references to your sub projects:

<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>X</groupId>
  <artifactId>X</artifactId>
  <version>0.1</version>
  <name>X</name>

  <packaging>pom</packaging>

  <modules>
    <module>project_A</module>
    <module>project_B</module>
    <module>project_C</module>
  </modules>

  <build>

    <defaultGoal>package</defaultGoal>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.3</version>
          <configuration>
            <descriptors>
              <descriptor>src/assemble/bin.xml</descriptor>
            </descriptors>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

  </build>

</project>

Put this in a directory that contains those 3 directories in which each of your projects are. Then you can call mvn clean package on this directory and Maven will magically solve the problem for you, building each project in the right order and making one project available to the others if there are dependencies.

BONUS: On the example above I am also adding a reference to a Maven Assemble, which you can use to pack all your projects into a single distribution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM