I am currently switching from ant to maven for a larger library consisting of a couple of projects. I am pretty new to Maven.
Due to the fact, that multiple libraries require the same external files as input for testing, I decided to create an individual project containing the resources (Project1), as explained here. Project1 only contains the test resources, nothing else, except an class project1.java with an empty main method. The resource files are ASCII text and binary files with arbitrary file extensions. Now, I want to use these files in testing of other projects, e.g. Project2. I tried to illustrate my structure:
Root
|-Project1: Resources
| |-src
| | |-main
| |-test
| |-resources
| |-file1.txt
| |-file2.dat
| |-...
|-Project2: Consumer
| |-src
| | |-main
| |-test
| |...
| |-test1.java (requires file1.txt)
| |-test2.java (requires file2.dat)
| |-...
I added Project1 as a dependency in the POM of Project2. Both projects are built with Clean and Build from Netbeans 11, so I guess mvn clean, build & install.
I do test for the existance of the relevant file in Project1 with Project1.class.getClassLoader().getResource([FILE]) and all files seem to be at the correct location. However, if I use Project1.class.getClassLoader().getResource([FILE]) from within Project2, e.g. test1.java the respective FILE is not found.
System.out.println("1:" + Project1.class.getClassLoader().getResource(""));
System.out.println("2:" + Project1.class.getClassLoader().getResource("file1.txt"));
from within test1.java in Project2 gives:
1: file:[PATHTOPROJECT]/Project2/target/test-classes/
2: null
So I tried following the apporach in this description. My problem with the explanation is, that I do not have a plugin to perform the executions for maven-shared-archive-resources as described in the consumer-part. Thus, I do not know where to put them. In the example the executions are carried out inside <groupId>org.codehaus.mojo</groupId> with <artifactId>sql-maven-plugin</artifactId>.
I also had a look at this thread, but I get an Ant BuildException.
Could somebody be so kind and explain a maven novice, how I can share resources between different projects? Is there even another, simpler way?
POM.xml of Project2
I tried to strip the POM.xml down to a MWE.
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!-- TOOL INFO -->
<groupId>org.test</groupId>
<artifactId>project2</artifactId>
<version>0.0.0.1</version>
<packaging>jar</packaging>
<!-- PROPERTIES -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- System -->
<java.version>1.8</java.version>
<junit.jupiter.version>5.3.1</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<maven.plugin.compiler.version>3.8.1</maven.plugin.compiler.version>
<maven.plugin.enforcer.version>3.0.0-M2</maven.plugin.enforcer.version>
<maven.plugin.release.version>2.5.3</maven.plugin.release.version>
<maven.plugin.resources.remote.version>1.6.0</maven.plugin.resources.remote.version>
<maven.plugin.surefire.version>3.0.0-M3</maven.plugin.surefire.version>
<!-- Own -->
<project1.version>0.0.0.1</project1.version>
</properties>
<!-- DEPENDENCIES -->
<dependencies>
<dependency>
<groupId>org.test</groupId>
<artifactId>project1</artifactId>
<version>${project1.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- BUILD -->
<build>
<!-- PLUGINS -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.plugin.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven.plugin.enforcer.version}</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<requireJavaVersion>
<version>${java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>${maven.plugin.release.version}</version>
<configuration>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.plugin.surefire.version}</version>
</plugin>
</plugins>
</build>
</project>
Change the location of the resources in project 1. Note, moved from test dir to main.
|-Project1: Resources
| |-src
| | |-main
| |-resources
| |-file1.txt
| |-file2.dat
| |-...
Then, in project 2's POM, add project 1 as a test dependency:
<dependency>
<groupId>org.test</groupId>
<artifactId>project1</artifactId>
<version>${project1.version}</version>
<scope>test</scope>
</dependency>
Instead of sharing resources, create another project called "onlyresources" or something like this and inside the project keep only those *.txt, *.dat files. Other projects will have the dependency on the above defined project called "onlyresources". In case of multi module maven project, ensure to add the dependency to other projects. This project I mean "onlyresources" should be the first project which should be built first.
Related
I have not followed the general Maven project structure in my Project. This is how my project structure looks like -
ProjectName
|- src
|- app
|- models
|- services
|- test
|- unit
|- services
|- integration
|- services
For the test I am using Junit and Mockito. My pom.xml file looks like this -
<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>ProjectName</groupId>
<artifactId>ProjectName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The problem that I am facing now is, when I am running my tests it is not able to find the dependency packages junit and mockito. I know it is because I have declared the scope of these dependecies as test.
What I want to know is, what change should I make in my pom.xml file so that Maven can indentify my test directory?
The Maven surefire plugin will look for tests in the directory the property project.build.testSourceDirectory is pointing to.
So you can add this to your pom.xml to change the value of this property:
<build>
<testSourceDirectory>${project.basedir}/src/test</testSourceDirectory>
</build>
This will execute all tests (unit and integration) in the test phase. If you dont want to execute the integration tests, you can set the property to ${project.basedir}/src/test/unit.
I would also suggest to follow the conventions for maven projects as others did.
Still, things should work by overriding following maven properties as below:
<properties>
<!-- Considering src/app will have all your sourcr java code -->
<project.build.sourceDirectory>src/app</project.build.sourceDirectory>
<!-- Considering all your junit tests are in src/test/unit -->
<project.build.testSourceDirectory>src/test/unit</project.build.testSourceDirectory>
...
</properties>
If you want to add integration test with maven, then I would suggest to go over this article.
You can remove <sourceDirectory> and <resources> from <build> section.
Following is my project structure.
test-proj
|_ src
|_main
| |_java
|_test
|_java
|_prop.properties
|_pom.xml
And my 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<!-- The resources tag will be used if prop file is under src location. -->
<!-- <resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources> -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/prop.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
</project>
Prop.properties file has
spring-version=3.1.0.RELEASE
I have tried to use maven-read properties plugin to read the properties and substitute the same in the place of spring version. But it throws up error saying 'dependencies.dependency.version' for org.springframework:spring-beans:jar must be a valid version but is '${spring-version}'.
I tried to use validate phase instead of initialize in the maven execution phase. But still the problem persists. Instead of context path, i tried replacing the property file location to be an absolute path D:\test-proj\prop.properties in configuration which didnt help me either. I am using maven compiler plugin version 2.3.2. Am I missing something? Please let me know if its feasible to substitute dependency versions by some other plugin as well.
Note: I will not be able to use parent-child pom relationship as all my projects are modular and they don't depend on the same parent
If you want multiple unrelated project share dependecies version you can use Bill of Material concept http://howtodoinjava.com/maven/maven-bom-bill-of-materials-dependency/
Create unrelated BOM project with packaging pom. Add shared dependencies to the project
Each your project must import the BOM project
Below is snippet from the example page
How to add BOM [Bill Of Materials] dependency
Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
I cannot imagine that this is going to work. AFAIU Maven has to resolve dependencies prior to any build execution since how should it be able to create a proper classpath for executions otherwise?
Re:
I will not be able to use parent-child pom relationship as all my projects are modular and they don't depend on the same parent.
I think you are able. Establish a super-parent POM that contains appropriate setting(s) to be inherited by all and make all your projects' parents childs of this:
+- super-parent
+- pom.xml ... containing setting(s) for all
+- parent-project-1
+- ...
+- parent-project-2
+- ...
+- ...
I've just started using Maven for one of my Java projects. It took me a few edits to get Maven to accept the pom.xml but I finally got it working. However, to my surprise it still uses an older version of the pom.xml!
The thing is that I can't even find it in the project's directory. I used Notepad++ for all my edits. When I open pom.xml it's the correct (latest) version. When I look into the JAR with jd-gui, it's the previous version that Maven was complaining about. This makes no sense. I tried deleting everything in the target directory and running 'maven clean' to no avail.
Here's the current pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>my.project</groupId>
<artifactId>solint</artifactId>
<version>1.0</version>
<name>SolInt</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<!-- Prepares Agent JAR before test execution -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifestEntries>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Premain-Class>my.project.Agent</Premain-Class>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
</dependencies>
</project>
The only way I've been able to force Maven to use the latest version of pom.xml is to copy the whole project to a different directory. I want to understand this mystery. Any ideas?
EDIT 1:
Here's how the JAR looks after mvn package. Note that this one has the correct pom.xml because I changed the root directory.
If you deleted everything, then the next copy of the jar file built should contain the at-that-time current pom.xml file.
However, if you are actually loading the JAR file out of the ~/.m2/repo cache, then you need to do a maven install to update the cache, and then the projects that use that JAR will get the last installed version.
I suggest you to take a look here to find out where your pom.xml should be located inside your folder structure.
Another important thing is to understand how your folders structure should be; for this, please take a look here.
Basically, you should have something like this:
+ Project Folder
|
+ src
|
+ main
|
+ java
|
+ {here goes your package, classes, etc.}
|
i.e.
|
+ com
|
+ sample
|
+ HelloWorld.java
|
+ pom.xml
Also it's important, that you can have poms inheriting from other Poms and you also could have aggregation to group several poms on a project, check this to get more info.
Also, I will suggest you to use a IDE instead just Notepad++, such IntelliJ or Eclipse; both have Maven Integration.
I am using intellij idea ultimate version 12.1.4 and trying to use maven with a project. Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.fasterxml</groupId>
<artifactId>oss-parent</artifactId>
<version>10</version>
</parent>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
<name>jackson-databind</name>
<description>General data-binding functionality for Jackson: works on core streaming API</description>
<url>http://wiki.fasterxml.com/JacksonHome</url>
<scm>
<connection>scm:git:git#github.com:FasterXML/jackson-databind.git</connection>
<developerConnection>scm:git:git#github.com:FasterXML/jackson-databind.git</developerConnection>
<url>http://github.com/FasterXML/jackson-databind</url>
<tag>jackson-databind-2.2.2</tag>
</scm>
<properties>
<osgi.export>
com.fasterxml.jackson.databind,
com.fasterxml.jackson.databind.annotation,
com.fasterxml.jackson.databind.cfg,
com.fasterxml.jackson.databind.deser,
com.fasterxml.jackson.databind.deser.impl,
com.fasterxml.jackson.databind.deser.std,
com.fasterxml.jackson.databind.exc,
com.fasterxml.jackson.databind.ext,
com.fasterxml.jackson.databind.introspect,
com.fasterxml.jackson.databind.jsonschema,
com.fasterxml.jackson.databind.jsonFormatVisitors,
com.fasterxml.jackson.databind.jsontype,
com.fasterxml.jackson.databind.jsontype.impl,
com.fasterxml.jackson.databind.module,
com.fasterxml.jackson.databind.node,
com.fasterxml.jackson.databind.ser,
com.fasterxml.jackson.databind.ser.impl,
com.fasterxml.jackson.databind.ser.std,
com.fasterxml.jackson.databind.type,
com.fasterxml.jackson.databind.util
</osgi.export>
<osgi.import>
com.fasterxml.jackson.annotation,
com.fasterxml.jackson.core,
com.fasterxml.jackson.core.base,
com.fasterxml.jackson.core.format,
com.fasterxml.jackson.core.json,
com.fasterxml.jackson.core.io,
com.fasterxml.jackson.core.util,
com.fasterxml.jackson.core.type,
org.xml.sax,org.w3c.dom, org.w3c.dom.bootstrap, org.w3c.dom.ls,
javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
</osgi.import>
<!-- Generate PackageVersion.java into this directory. -->
<packageVersion.dir>com/fasterxml/jackson/databind/cfg</packageVersion.dir>
<packageVersion.package>com.fasterxml.jackson.databind.cfg</packageVersion.package>
</properties>
<dependencies>
<!-- Builds on core streaming API; also needs core annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.2</version>
</dependency>
<!-- and for testing, JUnit is needed, as well as quite a few
libs for which we use reflection for code, but direct dep for testing
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.7.9</version>
<scope>test</scope>
</dependency>
<dependency> <!-- from core we just test for repackaged cglib, not hibernate proper -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-cglib-repack</artifactId>
<version>2.1_3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<excludes>
<exclude>com/fasterxml/jackson/failing/*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${javadoc.version}</version>
<configuration>
<links>
<link>http://docs.oracle.com/javase/6/docs/api/</link>
<link>http://fasterxml.github.com/jackson-annotations/javadoc/2.1.1/</link>
<link>http://fasterxml.github.com/jackson-core/javadoc/2.1.1/</link>
</links>
</configuration>
</plugin>
<plugin>
<!-- Inherited from oss-base. Generate PackageVersion.java.-->
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<executions>
<execution>
<id>process-packageVersion</id>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<properties>
<maven.test.skip>true</maven.test.skip>
<skipTests>true</skipTests>
</properties>
</profile>
</profiles>
</project>
When I do a maven dependency resolution, its taking forever to get it through and this is what I get:
And it sticks here forever.
Here are my maven settings:
I followed steps given in this SO answer.
Please let me know where am I doing wrong and how do I get rid of it.
EDIT
If this should help, I am trying to run this code: https://github.com/hmkcode/Android/tree/master/java-post-gcm
I am not sure whether its an issue with the pom.xml itself. Perhaps if someone is able to have it working on his IDE, it may be concluded that its an issue with my specific IDE config.
If this is an issue very specific to intellij, I would suggest do the following steps
You can try running mvn clean install.(Depending on your network speed it will pull every dependency)
Change intellij settings in
Preferences->build,execution,deployment->maven->Maven home directory->{now change this to point to your mvn home rather than bundled mvn}
mvn -v in command line should give you the maven home path.
Now it won't try to download dependencies again.
File | Settings | Build, Execution, Deployment | Build Tools | Maven |
Threads (-T option) | 8 (or however many threads you want to try)
mvn compile -T 8
I had the same issue. Maven was taking an hour in IntelliJ and also running from the command line. Using the -T parameter you can tell Maven to use more threads to download artifacts simultaneously. Takes only about 15 minutes now.
Don't use bundled mvn. Point "Maven home directory" to the path on your file system.
After you're done building. Check "Work offline". This stops mvn from searching remote websites each time you build. Uncheck when you update your POM file so it can search for the changes.
You can also skip tests. This is evertyhing from the command line.
mvn clean install -T 8 -o -DskipTests
Select File, Settings, then set VM options for Maven importing to -Xms1024m -Xmx2048m.
Visit https://www.programmersought.com/article/56414660398/
For me, issue was related to https://repo.maven.apache.org/maven2 and it was not responding.
I added another mirror(maven-central.storage.googleapis.com) in settings.xml
I've a web application configured with Maven which uses a library, also configured with Maven and when I package geronimo-servlet_3.0_spec-1.0.jar is included in WEB-INF/lib and I don't understand why.
I check the library with mvn dependency:tree
$ mvn dependency:tree | grep geronimo
[INFO] +- org.apache.geronimo.specs:geronimo-servlet_3.0_spec:jar:1.0:provided
I check my web app:
$ mvn dependency:tree | grep geronimo
$
However when I run mvn:package the file gets included in WEB-INF/lib.
When I run mvn tomcat:run I can see:
INFO: validateJarFile(/home/stivlo/workspace/private/src/main/webapp/WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Why and how to avoid? Thank you.
UPDATE 1: as requested I add the 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>
<groupId>org.obliquid</groupId>
<artifactId>test-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>private webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!-- For Jakarta ORO -->
<repository>
<id>mvnsearch</id>
<name>Maven Search</name>
<url>http://www.mvnsearch.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>private</finalName>
</build>
</project>
UPDATE 2: I followed the advice of Stephen C and modified the build section as follows:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war</artifactId>
<version>2.1</version>
<configuration>
<overlays>
<overlay>
<groupId>org.obliquid</groupId>
<artifactId>test-webapp</artifactId>
<excludes>
<exclude>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
However geronimo*.jar still gets included. I guess I've made a mistake in this configuration.
UPDATE 3: Stephen C. says that I should use
the groupId the artifactId of the WAR
file that contains the JAR file(s)
that you are trying to exclude.
I didn't know that WAR files could have a groupId and artifactId, in fact in my pom.xml I don't see any. My project builds a WAR file and has a groupId and an artifactId and those were the ones I tested above without success.
The dependency causing the problem is the following (is a JAR, not a WAR):
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
If I try to use the groupId and artifactId listed in this dependency I've the following error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.obliquid.helpers:obliquid-helpers]
is not a dependency of the project. ->
[Help 1]
If I try to use the groupId and artifactId of the JAR included by org.obliquid.helpers:
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>)
I have the same error.
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.apache.geronimo.specs:geronimo-servlet_3.0_spec]
is not a dependency of the project. ->
[Help 1]
Reading the War plugin documentation, I found a section about creating skinny WARs. So I tried the following:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Still without any success, geronimo-servlet_3.0_spec-1.0.jar is still there!
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.obliquid.helpers:obliquid-helpers] is not a dependency of the project. -> [Help 1]
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.apache.geronimo.specs:geronimo-servlet_3.0_spec] is not a dependency of the project. -> [Help 1]
UPDATE 4: I discovered that the target/private.war file is not a zip of target/private/ directory, but the exclusions are done at packaging time and not by deleting files in target/private/ -- This means, I've to re-test all the things I did before.
Suggestion of gouki: doesn't work, the JAR is still there also in the WAR file.
Suggestion of Stephen C., maybe mis-understood: actually I just noticed that the pom.xml is always invalid whatever groupId/artifactId I put of the three possibilities explained above. So they didn't work for me.
What I found in the documentation (packagingExcludes), works.
Now, if I had to choose one of he answers I would choose Stephen C., because he helped me pointing at the documentation of the WAR plugin (I was reading in the wrong places). However I'd accept an answer that doesn't work, at least in the way I tried (probably wrong). So I'm not going to accept any answer, and add a new answer myself with the final working configuration.
UPDATE 5: I post the relevant part of the pom.xml of obliquid-helpers, that mentions geronimo-servlet_3.0_spec. I've marked it optional and with scope provided, still it gets included by a web-app, unless I mark it as "packagingExclude" in the maven-war-plugin configuration.
<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>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<packaging>jar</packaging>
<name>obliquid-helpers</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
[...]
</repositories>
<dependencies>
[...]
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Clearly, something has a dependency on that JAR file. If it is not showing up in the dependency tree, perhaps it is due to a dependency of your webapp WAR file on another WAR file that has this dependency.
If that is the case, then you could get add an <excludes> to the <overlay> element of the build descriptor for the WAR file plugin; e.g.
...
<build>
<finalName>webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<overlays>
<overlay>
<groupId>xxx</groupId>
<artifactId>yyy</artifactId>
<excludes>
<exclude>WEB-INF/lib/whatever.jar</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
If you are using WAR file overlays, you should always include the clean target in the build. Otherwise you can get old dependencies hanging around in the WAR file. (IIRC, there is a Warning in the Maven output each time you build an overlaid WAR without cleaning!)
In fact, this could be the root cause of your problems. For instance, if previously you had the "geronimo" as an ordinary dependency and you haven't run mvn clean since then, the JAR file could still be hanging around.
Based from your pom.xml, the only dependency that might have dependency on geronimo servlet is
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Can you try excluding the geronimo in this dependency?
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
First I want to thank gouki and Stephen C. for helping me. However their proposed solution didn't work for me. I'm grateful to them, but I can't accept their answer, because it would be misleading since it didn't work for this problem. I've upvoted Stephen C. answer, because he pointed me to the right documentation, which was essential to solve the problem.
Reading the WAR plugin documentation, especially the war:war mojo section, I've found an example on how to create Skinny WARs, which did the trick. So here is below the working configuration, to be added to the build section:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
The archive part is probably not really needed, but I will find out when I deploy the WAR. The part that does the trick is the packagingExcludes tag, that can contain a comma separated list of tokens to exclude from the WAR before packaging.