maven-rar-plugin includes all Dependencies with configuration set to false - java

I am using maven-rar-plugin and following is my configuration in POM file. includeDependencies is set to false. But all the dependent and dependent project's sub dependencies are all packaged into rar.
<plugin>
<artifactId>maven-rar-plugin</artifactId>
<version>2.4</version>
<configuration>
<raXmlFile>src/main/resources/META-INF/ra.xml</raXmlFile>
<includeDependencies>false</includeDependencies>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
</archive>
</configuration>
</plugin>
I cannot use exclusion in dependencies as it would lead to compilatin errors.
<dependencies>
<dependency>
<groupId>com.fi.ps</groupId>
<artifactId>frm-fl</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Is this a bug in the Maven plugin or is there a different way of configuring for rar packaging?

There is no includeDependencies parameter for the maven-rar-plugin and, from reading the source code, it isn't currently possible to exclude dependencies of the project. A possible work-around would be to declare the dependencies to exclude with the provided scope: they will be present during compilation but excluded when building the RAR file.
But why do you want to exclude dependencies in the first place? The maven-rar-plugin is used to build a Resource Adapter Archive file for the Java 2 Connector Architecture. Dependencies are supposed to be included, otherwise it won't work. Beware that, as stated in the FAQ, this plugin does not create compressed file like WinRar.

Related

How to exclude certain dependencies from being added to the Class-Path in maven?

I'm using maven to build an executable JAR and I want to add all dependencies minus a select few to the Class-Path of my MANIFEST.MF. So far I'm using the maven-jar-plugin for this:
<!-- Add dependent JARs to the classpath -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libraries</classpathPrefix>
<mainClass>MyMainClass</mainClass>
</manifest>
<manifestEntries>
<Built-By>Me</Built-By>
</manifestEntries>
</archive>
</configuration>
</plugin>
This works for adding all dependencies. However, I furthermore want to exclude certain dependencies from being added to the Class-Path. According to the documentation you can use the exclude tag as follows:
<configuration>
<!-- ... -->
<excludes>
<exclude>**selenium*</exclude>
</excludes>
<!-- ... -->
</configuration>
I would expect this to exclude any dependency which has selenium in the name but it does not work. For example I would like to exclude libraries/selenium-json-4.0.0-alpha-6.jar from the Class-Path. Even if I specify that exact name it does not exclude anything. I would also like to provide the groupId for exclusion similar to how the maven-dependency-plugin's excludeGroupIds tag works.
How can the desired Class-Path management be done using maven?
I'm also using maven-shade-plugin for building the executable (fat) JAR but its manifest manipulation facilities seem lesser known/documented. Setting the Class-Path using maven-shade-plugin via this answer works but how can I populate my dependencies with exclusions instead of hard-coding everything?
TL;DR
The includes and excludes parameters of the Maven JAR Plugin work on files, not on dependencies.
Longer version
Selenium is typically used as a test dependency. Is Selenium declared as a Maven dependency? If it's only used for tests, I would expect it to have <scope>test</scope>. In that case, it will not end up in the JAR manifest.
If you don't use Selenium as a test dependency and still don't want it to end up in the manifest, you may consider adding <scope>provided</scope>. Provided dependencies don't end up in the JAR manifest either. This would also mean that at runtime, as soon as you invoke a Selenium API it will probably crash...

Maven project doesn't recognize any classes from included sibling dependency [duplicate]

I am writing a project for acceptance testing and for various reasons this is dependent on another project which is packaged as a WAR. I have managed to unpack the WAR using the maven-dependency-plugin, but I cannot get my project to include the unpacked WEB-INF/lib/*.jar and WEB-INF/classes/* to be included on the classpath so the build fails. Is there a way to include these files into the classpath, or is there a better way of depending on a WAR?
Many thanks.
There's another option since maven-war-plugin 2.1-alpha-2. In your WAR project:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
This creates a classes artifact which you can use in the acceptance tests project with:
<dependency>
<groupId>your-group-id</groupId>
<artifactId>your-artifact-id</artifactId>
<version>your-version</version>
<classifier>classes</classifier>
</dependency>
Indeed, by design, Maven doesn't resolve transitive dependencies of a war declared as dependency of a project. There is actually an issue about that, MNG-1991, but it won't be solved in Maven 2.x and I'm not sure that I don't know if overlays allow to workaround this issue. My understanding of the suggested solution is to duplicate the dependencies, for example in a project of type pom.
(EDIT: After some more digging, I found something interesting in this thread that I'm quoting below:
I have been helping out with the development of the AppFuse project over
the last month where we make heavy use of the war overlay feature in the
Maven war plugin. It is a really nifty feature!
To get max power with war overlays I have developed the Warpath plugin
that allows projects to use war artifacts as fully fledged dependencies.
In brief:
1) The contents of the /WEB-INF/classes directory in the war dependency
artifacts can be included in the project's classpath for normal compile,
etc tasks.
2) Transitive dependencies from the war dependency artifacts become
available for use by other plugins, e.g. compile and ear - so no more
having to include all the dependencies when creating skinny wars!
The plugin has now been actively used in the AppFuse project for the
last few months, and I feel it is at a point where it is both usable and
stable.
Would the war plugin team be interested in including the warpath
functionality inside the war plugin? It would seem to be the most
natural place to host it.
So, I don't have any experience with it, but the maven warpath plugin actually looks nice and simple and is available in the central repo. To use it,include the following plugin configuration element in your pom.xml file:
[...]
<build>
<plugins>
<plugin>
<groupId>org.appfuse</groupId>
<artifactId>maven-warpath-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>add-classes</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
And add the war dependencies you want included in the classpath as warpath type dependencies:
[...]
<dependencies>
<dependency>
<groupId>org.appfuse</groupId>
<artifactId>appfuse-web</artifactId>
<version>2.0</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.appfuse</groupId>
<artifactId>appfuse-web</artifactId>
<version>2.0</version>
<type>warpath</type>
</dependency>
</dependencies>
[...]
Both the war and warpath dependency types are needed: the war type is used by the Maven war plugin to do the war overlay, the warpath type is used by the Warpath plugin to determine the correct list of artifacts for inclusion in the project classpath.
I'd give it a try.)
Use overlays. First, your test project need to have also packaging war.
Declare dependency of war project you want to test:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>your-project-arftifactId</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
then configure maven-war-plugin overlay:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webresources</directory>
<filtering>true</filtering>
</resource>
</webResources>
<overlays>
<overlay/>
<overlay>
<groupId>your.group</groupId>
<artifactId>your-project-artifactId</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
In the above example in test project I overwrite webresources configuration files (like conxtext etc.).
EDIT: This solution wasn't tested with Maven 3.
Good point, Justin. That got me actually solving my problem, namely: including a war into an assembly AND including all its transitive dependencies.
I could not duplicate the war-dependency as 'jar' as you suggested since the assembly plugin would not find a jar referenced by that groupId/artefactId, but
duplicating the war-dependency as type pom
works!
The war and its transitive dependencies are not included in the assembly.
To exclude the (now also appearing) pom file I had to add an exclude element like this:
<excludes>
<exclude>*:pom</exclude>
</excludes>
into my assembly.xml file.
I think this could also be a workaround for the original question of this thread.
If you list the dependency on the war project as a jar dependency it seems to pickup the required jars/resources. I'm using Maven 2.2 + m2eclipse.

maven-war-plugin : exclude many dependencies from lib folder

I'm using maven-war-plugin to generate a WAR file.
In the dependency hierarchy, I can see many transitive dependencies, which are extract in the lib folder.
After many research, I saw that the easiest way to exclude them from the war lib folder is to declare them as 'provided' in my dependencies.
However, I have a lot of dependencies to exclude, and I have to do this in many WAR pom file.
My question is :
Is there a way to group all these dependencies in a 'pom' packaging, and use this new artifact in my WAR pom file ?
If I understand your needs...
try this--> http://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html
when you build the war you can exclude all dependencies you want in this way:
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!--
Exclude JCL and LOG4J since all logging should go through SLF4J.
Note that we're excluding log4j-<version>.jar but keeping
log4j-over-slf4j-<version>.jar
-->
<packagingExcludes>
WEB-INF/lib/commons-logging-*.jar,
%regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
</packagingExcludes>
</configuration>
</plugin>
</plugins>

For which Maven dependency scopes is the dependency included in the compiled project?

E.g. if I make the BukkitApi jar a dependency for a maven project with depenecy scope set to provided,compile,system, runtime or test
In which scopes will the bukkitAPI be included in the compiled output?
Short version: By default, maven output (in the default target directory) does not include anything except the compiled code for the current project/module. That is, nothing from dependencies.
Long(er) version: with default jar packaging and no custom phase configuration. here is how maven behaves on a java project:
the compile phase : the .java files in the src/main/java/ directory get compiled to .classes files in the target directory. Dependencies for the compile scope get downloaded to your local repository.
the package phase : same as 1, plus you'll get a jar file in the target directory
the install phase : same as 2 you'll get a jar file in your local repository.
So, .jar files from dependencies are not included in anything by default !
So, how do I include dependencies in my "output", and what does thoses scopes mean ?
Now, using, for exemple, the assembly plugin to include dependencies in the output of the package phase (see Including dependencies in a jar with Maven), you'll normally get this default behavior:
provided : not included
compile (default) : included
system : not included
runtime : included
test : not included
Checkout this link for reference.
EDIT: Just try out this pom with the different scope values on guice, and you'll see that dependencies are included in the fake-1.0-SNAPSHOT-jar-with-dependencies.jar when scope is compile and runtime (this example does not need any source files)
<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.linagora</groupId>
<artifactId>fake</artifactId>
<version>1.0-SNAPSHOT</version>
<name>fake</name>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>2.0</version>
<scope>compile</scope>
<!-- system scope needs 'systemPath' attribute as well
<systemPath>/path/to/guice/guice-3.0.jar</systemPath>
<scope>system</scope>
-->
<!-- <scope>runtime</scope> -->
<!-- <scope>test</scope> -->
<!-- <scope>provided</scope> -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
That's not how Maven works. The dependencies just specify the classpath (for compilation, run time, testing). But the dependencies are not included in the output by default. You will have to ship all the dependency jars (at least the ones with scope compile and runtime).
Have a look at the dependency plugin. It provides goals to copy the dependencies.
To create a bundle for shipment, have a look at the assembly plugin (e.g. to create a zip file). It even provides a way to create an all-in-one jar, if that is what you're after.

Can't get the maven-war-plugin to exclude jaxb jars

I'm trying to use the <packagingExcludes> of the Maven war-plugin.
This is my configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.artifactId}-${project.version}-r${buildNumber}</Implementation-Version>
<Implementation-Buildtime>${timestamp}</Implementation-Buildtime>
</manifestEntries>
</archive>
<packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>
</configuration>
</plugin>
In my understanding this line:
<packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>
Should exclude all jars starting with 'jaxb' from the built .war file.
However after I run clean install I get both:
jaxb-api-2.1.jar
jaxb-impl-2.1.3.jar
Packaged in my .war WEB-INF/lib dir.
I'm using Maven 3.
Any help greatly appreciated.
Thanks in advance,
To answer gkamal's comment.
When I run mvn war:war -X I can see:
[DEBUG] Processing: jaxb-api-2.1.jar
[DEBUG] + WEB-INF/lib/jaxb-api-2.1.jar has been copied.
[DEBUG] Processing: jaxb-impl-2.1.3.jar
[DEBUG] + WEB-INF/lib/jaxb-impl-2.1.3.jar has been copied.
Also
[DEBUG] Excluding [WEB-INF/lib/jaxb*.jar] from the generated webapp archive.
No, exceptions, warning or errors or nothing that looks suspicious, anything specific I should look for ?
For a transitive dependency, you can use the exclusions element to exclude it.
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api-2.1</artifactId>
</exclusion>
</exclusions>
</dependency>
As gkamal commented, you could also add an explicit dependency on jaxb and set its scope to provided, this will override the scope of the transitive dependency so it is no longer packaged.
Another alternative, the war plugin also allows to exclude based on regular expressions, but the syntax is a bit more involved, the following snippet should exclude everything under lib whose filename starts with "jaxb":
<packagingExcludes>%regex[WEB-INF/lib/jaxb.*]</packagingExcludes>
This means you have them as dependency in your project so they will be packaged into the war. Just remove the dependencies so they wont be packaged anymore.
Based on the documentation of the option you used you have to use regex which means you should write:
<packagingExcludes>WEB-INF/lib/jaxb.*</packagingExcludes>
instead of
<packagingExcludes>WEB-INF/lib/jaxb*.jar</packagingExcludes>
Further to #gkamal's comment to your question (08/05/12#11:52), check your maven-war-plugin's version. I've just spent 2hrs looking at this issue myself to exclude an unknown transient to javaee-api*.jar.
With maven 3.0.4, I was defaulted to maven-war-plugin version 2.1 (you can tell if you run your build in debug - e.g. mvn clean package -X). One of your other comments is correct in saying package-excludes came in after this. See the war plugin page for details (although the actual page describing the entry doesn't indicate version info which is pretty poor as that's what you first search for).
If you update to maven-war-plugin to 2.3 (add <version>2.3</version>, your packaging-excludes should be used. Note though you will only see this in the built war's WEB-INF/lib, not in the transient war directory (which includes the excluded jars even if debug says they're to be excluded, which is very confusing when looking at this issue).
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
...
<packagingExcludes>
WEB-INF/lib/whatever-*.jar,
WEB-INF/lib/javaee-api-*.jar
</packagingExcludes>
...
</plugin>
...
However, in terms of best practice, this is probably a last gasp effort to exclude jars fromo the war and the dependency-level exclusions for transient jars is probably the most precise and correct way. That said, what if a transient jar is being brought in by multiple dependencies?
So, with the version upgrade, I think #khmarbaise's solution is fine (and the comment indicating it won't work is wrong). However, I think best practice is to use dependency-level exclusions as per your accepted answer.
You can do this by specifying inside <packagingExcludes></packagingExcludes> inside </configuration><configuration>.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<packagingExcludes>
WEB-INF/lib/ex1-*.jar,
WEB-INF/lib/ex2-logging-*.jar
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can specify path by wild cards and regular expressions too. See this link for more info.

Categories