How to exclude jars from maven dependency when packaging? - jboss7.x

I'm trying to package a jboss AS7.5 module (RedHat EAP 6.4) that includes a valve. That valve requires a binding to the AuthenticatorBase that comes with the tomcat-catalina-x.y.z.jar, which does not (to my knowledge) come as a downloadable module in its own right. So, I need to build that jar into my module, using the maven plugin for packaging with dependencies.
Problem is, the GAV for the dependency, org.apache.tomcat:tomcat-catalina:x.y.z, includes libraries that ARE included in AS, namely the servlet libraries. If I package the catalina dependency, using the maven plugin that does that, I get a bunch of unnecessary jars that break my module at runtime.
Is there a way to
only package the jar(s) I want?
find a different dependency that includes just the tomcat valve (i.e., the jar containing org.apache.catalina.authenticator.AuthenticatorBase) binding?
or, preferably, is there a module that already exists for exactly this purpose, one that I can reference as a module dependency in my module.xml file?
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat-catalina.version}</version>
</dependency>
...
</dependencies>
includes jars: tomcat-catalina, tomcat-servlet, tomcat-juli, tomcat-annotations, etc.
<build>
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default. -->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
How to get only tomcat-catalina.jar in my packaged module?

Related

how to put dependency in maven not in WEB-INF/lib

Is there a way to put dependency of a war not in WEB-INF/lib but in out custom folder?
I'm using Maven 2
Hi, what i need is that from list of dependencies of a war only one artifact should be placed not in WEB-INF/lib but in WEB-INF/bundles/ and others dependencies should be places in WEB-INF/lib
thx
THX to everybody, i can't update to Maven 2.1 so i did it through maven-antrun plugin )
I think what you need to do is make the dependency provided and copy it using mvn copy-dependencies. Here is an example that does that with apache commons / lang:
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copydep</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<inherited>false</inherited>
<configuration>
<includeArtifactIds>commons-lang</includeArtifactIds>
<outputDirectory>$project.build.directory/${project.build.finalName}/web-inf/bundles</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
($project.build.directory/${project.build.finalName} is the work folder that is used to assemble the war)
Maven war plugin supports extensive customization. Not clear from your question, what exactly you want, but this page should hopefully help.
Where do you want to put these libraries? You have to be carefull, as the war structure defines the WEB-INF/lib as the directory for third-parties libraries. All *.jar files located in this directory will be loaded in the Classpath by the container (such as Tomcat).
Changing this directory can be harmful for your application!
That's why I am not aware of a configuration parameter for the Maven WAR Plugin that allow you to change this WEB-INF/lib directory.
If you really know what you are doing, you can try to create your own WAR using an assembly.

yui compressor maven: A required class is missing: org.mozilla.javascript.ErrorReporter

I am not able to use yui-compressor maven plugin in my web app. When I run maven I get following error
[INFO] Internal error in the plugin manager executing goal 'net.sf.alchim:yuicompressor-maven-plugin:0.7.1:compress': Unable to load the mojo 'net.sf.alchim:
yuicompressor-maven-plugin:0.7.1:compress'
in the plugin 'net.sf.alchim:yuicompressor-maven-plugin'. A required class is missing: org.mozilla.javascript.ErrorReporter
Later I found that rhino js plugin contains this class org.mozilla.javascript.ErrorReporter. So I included this plugin in dependency tag but still I am getting the same error.
Has anyone came across such error.
--> updating main question to add the pom plugin details
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnWarning>true</failOnWarning>
<nosuffix>true</nosuffix>
<aggregations>
<aggregation>
<!-- remove files after aggregation (default: false) -->
<removeIncluded>false</removeIncluded>
<!-- insert new line after each concatenation (default: false) -->
<insertNewLine>false</insertNewLine>
<output>${project.basedir}/${webcontent.dir}/js/compressedAll.js</output>
<!-- files to include, path relative to output's directory or absolute path-->
<!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
<includes>
<include>**/autocomplete.js</include>
<include>**/calendar.js</include>
<include>**/dialogs.js</include>
<include>**/download.js</include>
<include>**/folding.js</include>
<include>**/jquery-1.4.2.min.js</include>
<include>**/jquery.bgiframe.min.js</include>
<include>**/jquery.loadmask.js</include>
<include>**/jquery.printelement-1.1.js</include>
<include>**/jquery.tablesorter.mod.js</include>
<include>**/jquery.tablesorter.pager.js</include>
<include>**/jquery.validate.js</include>
<include>**/jquery-ui-1.8.custom.min.js</include>
<include>**/languageDropdown.js</include>
<include>**/messages.js</include>
<include>**/print.js</include>
<include>**/tables.js</include>
<include>**/tabs.js</include>
<include>**/uwTooltip.js</include>
</includes>
<!-- files to exclude, path relative to output's directory-->
</aggregation>
</aggregations>
</configuration>
<dependencies>
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<scope>compile</scope>
<version>1.6R5</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.7</version>
<scope>provided</scope>
</dependency><dependency>
<groupId>net.sf.retrotranslator</groupId>
<artifactId>retrotranslator-runtime</artifactId>
<version>1.2.9</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
Could you try the latest version (1.1)?
The 0.7.1 version doesn't even seem to be on the official repository. Maybe a dependency resolution problem?
See the topic Yui compressor StringIndexOutOfBoundsException on jboss
The only way to use yuicompressor on web app is to manually merge it with rhino dependency. Otherwise, the app to run would require specifying required sequence of jars in classloader loading sequence (youcompressor must go before rhino).
I struggled with the ErrorReporter class missing too. I solved it by building a jar-with-dependencies which I then turned around to use in my web app,
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
Once I did that, everything worked. In my jar I could see that the org.mozilla.javascript.ErrorReporter.class was in there and Maven would then compile for me.

Maven copy project output into other project resources

There are two projects:
1) applet project that outputs jar file
2) web app project that should host the jar file.
After (1) finished building, the applet jar file should be copied into the webapp folder of (2). The purpose is that (2) will host the applet (1) on the Internet.
A lot of examples explain how to use another project as a library dependency. Other examples, show how to use ant plugin to copy files. I am unsure on how to properly set this up, so that 'mvn install' on the parent project will do the copying at the right time.
I would declare the applet as a dependency of the webapp, copy it to the webapp just before packaging using the Dependency plugin and its copy goal. The whole solution might looks like this:
<project>
...
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>my-applet</artifactId>
<version>${project.version}</version>
<scope>provided</scope> <!-- we don't want the applet in WEB-INF/classes -->
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>my-applet</artifactId>
<version>${project.version}</version>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<destFileName>the-applet.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</project>
Declaring the applet as dependency is for the reactor build order (but I'm not 100% sure it is required).

Building a WAR project with unzipped JAR dependency?

I have two projects, my-lib and my-webapp. The first project is a dependency of my-webapp. Thus, when ask Maven2 to build my WAR, the my-lib JAR is added in the WEB-INF/lib/ directory of the web application.
However, I want to have the my-lib JAR unzipped directly in the WEB-INF/classes directory, exactly as if the my-lib sources were contained in the project my-webapp.
In others words, instead of having the following WAR content:
my-webapp/
...
WEB-INF/
lib/
my-lib-1.0.jar
... (others third libraries)
I want to have that:
my-webapp/
...
WEB-INF/
classes/
my-lib files
lib/
... (others third libraries)
Is there a way to configure the my-webapp or the Maven2 war plugin to achieve that?
As blaufish's answer says, you can use the maven-dependency-plugin's unpack mojo to unpack an artifact. However to avoid the jar appearing in WEB-INF/lib, you need to not specify it as a dependency, and instead configure the plugin to unpack specific artifacts.
The following configuration will unpack the contents of some.group.id:my-lib:1.0:jar into target/classes during the process-resources phase, even if the artifact is not defined as a dependency. Be careful when doing this though as there is potential to clobber your actual content, this can cause much debugging.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-my-lib</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>some.group.id</groupId>
<artifactId>my-lib</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
</configuration>
</execution>
</executions>
</plugin>
You can configure the maven-dependency-plugin to just do that, unpack instead of copying a jar as explained here.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
<includes>**/*.class,**/*.xml</includes>
<excludes>**/*test.class</excludes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<excludes>**/*.properties</excludes>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
The unpack mojo seems to be close to what you are aiming for. Not sure how to complete the entire flow you are proposing though.
(btw, I am doubtful if this a good idea. utility classes should go into jars, and the jars are put either in the WAR or io the EAR. Unpacking utility jars seems wrong)
I was able to use the unpack mojo as described above, plus I marked the dependency itself as "provided" (scope) to avoid duplicating the jar contents under WEB-INF/lib.
[Oops, just realized that you were using Maven. I don't delete this answer because it may come to the rescue of some Ant user. So there's no need to mod me down...]
How many times to I have to mention that Jar, War and Ear Ant tasks are subtasks of the Zip one? :-) If I remember correctly, something like this would do the trick:
<war dist="my-webapp.war">
<zipgroupfileset dir="libs" includes="*.jar" prefix="WEB-INF/classes"/>
</war>
Also worth a trial is with src="mylib.jar" but I haven't tested this option.

Maven Assembly plugin - Include repo jars

I have a multi module project and I am using assembly plugin to build a tar file. I have included all of my modules in assembly plugin using moduleSets tag.
<moduleSets>
<moduleSet>
<includes>
<include>module1</include>
<include>module2</include>
All of my module jars are assembled into a lib folder. I want to add mysql jar from my local maven repository to the same lib folder that contains all of my other modules. Adding local repository jars in moduleset doesnt seem to work.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'mysql.mysql-connector-java:mysql-connector-java-version'
How can I include jars from maven repository.
I think that you need to declare your mysql jar in the includes subelement of a dependencySet.
Something like this:
<assembly>
<id>my-assembly</id>
...
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>mysql:mysql-connector-java</include>
</includes>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
...
</assembly>
Refer to the Descriptor Format documentation and/or the chapter 12.5.4. dependencySets Section of the Maven Book for more details on this element.
I don't think you need to do anything special, just make sure mysql is listed as dependency in your project and it should work. Same applied to dependencies on modules - just list them as dependencies. Below is typical configuration of assembly plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I had the same problem, and solved it by changing scope on pom dependency to compile.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>compile</scope>
</dependency>