Fine tuning Maven dependencies
March 22, 2010 02:55:04 Last update: March 22, 2010 03:48:55
If you followed the steps in Start a Java EE application with Maven, you'll arrive at an EAR file that's ready to be deployed.
However, if you look carefully, you'll find that the
Actually, the
Build again and you'll find that
But if you always pack
Since the ejb client jar is no longer packaged with webapp, there's no need to generate it either:
Dependency exclusions:
Maven packs all transitive dependencies in your final product. For example, by declaring struts as a dependency of the
However, if you look carefully, you'll find that the
ejb-1.0.jar file is included in the EAR file twice: once as the ejb module of the EAR, another time under the WEB-INF/lib folder of webapp-1.0.war. The second is brought about by specifying the ejb project as a dependency in the webapp POM.
Actually, the
webapp project is dependent on the ejb project as an EJB client. So we should have been more accurate by specifying the type of dependency as ejb-client, not ejb:
- Edit
webapp/pom.xml, change theejbdependency toejb-client:<project> <modelVersion>4.0.0</modelVersion> <groupId>maven-tutorial.java-ee</groupId> <artifactId>webapp</artifactId> <packaging>war</packaging> <version>1.0</version> <name>Java EE webapp</name> <parent> <groupId>maven-tutorial</groupId> <artifactId>java-ee</artifactId> <version>1.0</version> </parent> <dependencies> <dependency> <groupId>maven-tutorial.java-ee</groupId> <artifactId>ejb</artifactId> <version>1.0</version> <type>ejb-client</type> </dependency> <dependency> <groupId>struts</groupId> <artifactId>struts</artifactId> <version>1.2.9</version> </dependency> </dependencies> </project>
- Update ejb/pom.xml to generate ejb-client artifact (note the addition of the
buildsection):<project> <modelVersion>4.0.0</modelVersion> <groupId>maven-tutorial.java-ee</groupId> <artifactId>ejb</artifactId> <packaging>ejb</packaging> <version>1.0</version> <name>Java EE ejb</name> <parent> <groupId>maven-tutorial</groupId> <artifactId>java-ee</artifactId> <version>1.0</version> </parent> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <configuration> <generateClient>true</generateClient> </configuration> </plugin> </plugins> </build> </project>
Build again and you'll find that
ejb-1.0-client.jar is packaged with webapp, not ejb-1.0.jar.
But if you always pack
webapp and ejb together in the same ear, there's no need to include the ejb client jar in webapp at all. The scope of the ejb client dependency should be "provided" (by packaging in an EAR with ejb):
<dependency> <groupId>maven-tutorial.java-ee</groupId> <artifactId>ejb</artifactId> <version>1.0</version> <type>ejb-client</type> <scope>provided</scope> </dependency>
Since the ejb client jar is no longer packaged with webapp, there's no need to generate it either:
- The
buildsection in the ejb project should be removed - The dependency of
webapponejbshould be changed to:<dependency> <groupId>maven-tutorial.java-ee</groupId> <artifactId>ejb</artifactId> <version>1.0</version> <type>ejb</type> <scope>provided</scope> </dependency>
Dependency exclusions:
Maven packs all transitive dependencies in your final product. For example, by declaring struts as a dependency of the
webapp project, Maven puts a bunch of jar files under WEB-INF/lib, including xml-apis, xalan, antlr, etc. But these jars may already be available from your Java EE container, including them in your WAR may actually cause errors. You can use the exclusions tag to exclude these transitive dependencies:
<dependency> <groupId>struts</groupId> <artifactId>struts</artifactId> <version>1.2.9</version> <exclusions> <exclusion> <groupId>antlr</groupId> <artifactId>antlr</artifactId> </exclusion> <exclusion> <groupId>xalan</groupId> <artifactId>xalan</artifactId> </exclusion> <exclusion> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> </exclusion> </exclusions> </dependency>