In Maven, adding dependency is just a piece of cake. Take a look on the following pom.xml.
<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/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<!-- Project Details -->
<groupId>ykyuen</groupId>
<artifactId>project-apple</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>project-apple</name>
<dependencies>
<!-- project-apple depends on project-banana -->
<dependency>
<groupId>ykyuen</groupId>
<artifactId>project-banana</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Setting the above dependency is just the same as importing the project-banana.jar in project-apple.
Now i have another Maven web application project called project-orange with packaging type equals to war. Adding the above dependency linkage does not work at all since Java does not see .war file as a classpath.
To solve the problem, there are two approaches:
Create a Maven module which contains the classes of project-orange with jar packaging. Now you can treat the new Maven module as normal dependency.
Configure the maven-war-plugin such that it will build the .jar file when building the .war file. Add the following code under the node of your war project. The following is an example.
.
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
</configuration>
</plugin>
</plugins>
</build>
...
After running mvn install, you can find the following archive files in the target folder
- project-orange.war
- project-orange-classes.jar
Now you can edit the pom.xml of project-apple for adding the new dependency.
<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/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<!-- Project Details -->
<groupId>ykyuen</groupId>
<artifactId>project-apple</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>project-apple</name>
<dependencies>
<!-- project-apple depends on project-banana -->
<dependency>
<groupId>ykyuen</groupId>
<artifactId>project-banana</artifactId>
<version>1.0</version>
</dependency>
<!-- project-apple depends on project-orange -->
<dependency>
<groupId>ykyuen</groupId>
<artifactId>project-orange</artifactId>
<version>1.0</version>
<!-- To map the project-orange-classes.jar -->
<classifier>classes</classifier>
</dependency>
</dependencies>
</project>
reference: http://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…