简体   繁体   中英

Specify maven dependencies for packages

Is it possible in maven project to sepecify in the dependecies, which one is applied for which java package ? If it's possible how ?

Example: I have two package:

1) org.test.compute
2) org.test.stock

And dependencies are:

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.35</version>
        </dependency>
<dependency>
            <groupId>org.cloudfoundry</groupId>
            <artifactId>cloudfoundry-client-lib</artifactId>
            <version>1.0.0</version>
        </dependency>

my goal is:

aws-java-sdk applied to org.test.compute

and org.cloudfoundry to org.test.stock

You can achieve this by using a multi-module project in maven.

https://maven.apache.org/guides/mini/guide-multiple-modules.html

No, you can't. All packages of a Java application are included in the same runtime classpath, so all of the dependencies that are on the classpath are visible in all of the packages.

If it's only because there is two classes with the same name in the two dependencies, I suggest to use full qualified names when you're using the classes like:

org.cloudfoundry.pathA.classA
com.amazonaws.pathB.classA

Also, if the dependencies are only used in the test classes, you want to use the test scope like this:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk</artifactId>
  <version>1.9.35</version>
  <scope>test</scope>
</dependency>

By doing so, the dependecy will be only used while compiling and running the tests. It will not be included in the final WAR or EAR file.

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