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 is two approaches.
1. 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.
2. Configure the maven-war-plugin such that it will build the .jar file when building the .war file. Add the following code under the <project> 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>
Done =)
Reference:
Update @ 2011-06-23: update the maven-war-plugin xml. Thanks Sergei Vasilyev. =D
Thank you very much! I’m a Maven newbie and I was missing the attachClasses feature of the war plugin.
LikeLike
You are welcome.
Enjoy the power of Maven =)
LikeLike
Thank you for the pointer. Probably things have changed since v. 2.1-beta-1 of plugin, but Maven kept reporting to me from my dependent project that classes were not found unless I added the “classesClassifier” element to the referenced war project with the “classes” value (in the “configuration” section next to “attachClasses”)
LikeLike
Thanks for your update =)
LikeLike
Man! You really saved my day with this post. Thanks!
LikeLike
Good to know that i could help =)
LikeLike
You’re the man! Thank you very much for this post.
LikeLike
You are welcome. =D
LikeLike
Thanks for the post but i have another problem. I jave resources project-orange.war that i need to run (property file etc.) Currently if I start a class from project-apple, it can not find the resources of orange. What sholud i di?
LikeLike
Maybe you could try using the webResources parameter. It helps you to include resource files into the WEB-INF/classes.
Maven WAR Plugin – Adding and Filtering External Web Resources
LikeLike
Can we use the attachClasses tag of war plugin with Maven 1 ?
LikeLike
i am not sure as i haven’t tried that before. but i guess it should work in Maven 1.x as i found the war plugin name in the Maven 1.x project page.
Maven 1.x
LikeLike
Thanks very much for this.
I was struggling with the appfuse warpath plugin for a while, but this approach with the war plugin is perfect and works with m2e as well.
LikeLike
Thanks for your comment and good to know that i could help. =)
LikeLike
Thanks for this post – really clear and massively useful for me.
LikeLike
Thanks for your comment and good to know this post could help. =D
LikeLike
Great.Just what we needed. Thanks alot!
LikeLike
Thanks….I followed the second approach..I manage to resolve this issue for my proj .. but after adding the dependency it compiles fine and failed at runtime with classDefNotFoundException ? Is there anything else I need to do ?
LikeLike
I guess your runtime environment does not include the compiled jar file. check the error log and try to figure out which jar is missing.
LikeLike
how to add a war module dependent on another war module?
LikeLike
As mentioned by this post, you have to build the .jar file.
LikeLike
i have created .jar file and .war file. to the main project the created .jar is added to the WEB-INF/lib folder. But how can i use the jsps and resource file (struts.xml,applicationcontex.xml)
my resource files are in the folder WEB-INF/lib/xxxx.jar
LikeLike
i guess those files should be stored inside src/main/resources.
Struts 2 – Setup a Struts 2 Web Application in Maven
LikeLike
Is there a way to make the JSF (Facelet Pages) available to the parent Project?
LikeLike
What do u mean by available to the parent project? I think better to keep the JSF pages in the war project, any reason or aim for doing that?
LikeLike