Include specific JAR files in skinny WARs with Maven - maven

I've been having some problems with a WAR module and its difficulty in loading a taglib. I kept getting this exception:
JSPG0047E: Unable to locate tag library for uri http://www.springframework.org/tags/form
at com.ibm.ws.jsp.translator.visitor.tagfiledep.TagFileDependencyVisitor.visitCustomTagStart(TagFileDependencyVisitor.java:76)
at com.ibm.ws.jsp.translator.visitor.JspVisitor.processJspElement(JspVisitor.java:366)
at com.ibm.ws.jsp.translator.visitor.JspVisitor.processChildren(JspVisitor.java:419)
at com.ibm.ws.jsp.translator.visitor.JspVisitor.processJspElement(JspVisitor.java:369)
...
After a bit of searching, I found a lot of suggestions that the spring jars need to be on the application's classpath. I checked in my EAR's lib folder and sure enough, spring-web and spring-webmvc were there.
It should be noted that the EAR is built with skinny WARs - since they use most of the same libraries, all library files are in MyAppEAR/lib instead of MyAppEAR/MyWAR1/WEB-INF/lib, MyAppEAR/MyWAR2/WEB-INF/lib, MyAppEAR/MyWAR3/WEB-INF/lib, etc...
I did finally manage to resolve this missing taglib error, but I had to move spring-web and spring-webmvc to MyAppEAR/MyWAR1/WEB-INF/lib.
So I have a couple of questions:
Is this the only way to fix this problem?
If so, how can I build a sort-of skinny WAR using maven? Currently, the EAR part of the POM looks like this:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<applicationName>MyAppEAR</applicationName>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<skinnyWars>true</skinnyWars>
I guess I could turn off skinny WARs and then have some other step remove all libraries from the WAR files and copy them to MyAppEAR/lib except for the spring web jars, but I am hoping there's a better solution.

I had the same issues myself - I just couldn't access Spring or Sitemesh's TLDs when my spring JAR files were in my ear/lib folder!
Including the classpath in the MANIFEST was causing my app server go haywire (since all dependencies were being loaded twice).
(Yes, I also have skinnyWars set to true in my maven-ear-plugin).
The only way I could have gone round the issue was by including Spring and sitemesh by including them in maven-war-plugin's configuration:
<packagingExcludes>%regex[WEB-INF/lib/(?!spring|sitemesh).*.jar]</packagingExcludes>
Not the most elegant solution, but the least damaging I could find.
This is my full configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!--
Q. Why is this set?
A. maven-ear-plugin in our EAR modules have `skinnyWars` enabled to that WAR files
would not include any third-party libraries in the WAR's WEB-INF/lib folder, however
this does not work for ejbs (like our own EJB modules).
We'll need to exclude them from here anyway (except for a few select JARS)...
-->
<packagingExcludes>%regex[WEB-INF/lib/(?!spring|sitemesh).*.jar]</packagingExcludes>
<archive>
<manifest>
<addClasspath>false</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
PS. My setup is JBoss EAP 6.x and an ear file with several EJBs, WARs and third-party JARs.

I think I got it working.
In the WAR's POM file:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<packagingExcludes>WEB-INF/lib/*.jar,WEB-INF/*.xmi</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>../../WEB-INF/lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
This causes the generated WAR file to have a META-INF/MANIFEST.MF file with classpath entries that look like ../WEB-INF/lib/$someJarFile - that is the relative path from the WAR to the EAR's library folder. I guess the WAR needs to have the classpath specified, having the libraries in the EAR just isn't enough.

Related

Maven: Three Jars in one project. Trying to use an assembly, but not getting pom.xml and pom.properties embedded in the jars

I have a project that produces three different jar files:
Server.jar: This contains all classes and resources. A standard jar
Client.jar: This contains only a few external classes and no resources.
ServerSDK.jar: This contains all the classes, resources, test classes, and other configuration files.
I've decided to do all three jars in a single project, so a change in any of the sources spawns a Jenkins build and deploys all three at once. I build the Server.jar as my standard pom.xml jar. Then, I use assemblies to build the Client.jar and the ServerSDK.jar.
I have the assemblies that build the other two jars, and everything is 99% of the way I like it, but there is bit of munging I'd like to do.
We add a few entries in our MANIFEST.MF file to incorporate the Jenkins build information and project information.
In a standard Maven jar, the pom.xml and pom.properties are embedded in the META-INF directory.
The first one I have managed to do via the <assembly> configuration in my maven-assembly-plugin. The second one I can't seem to get to work even though I have <addMavenDescriptor> set to true in my <assembly> configuration.
Here's my maven-assembly-plugin section in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<manifestSections>
<manifestSection>
<name>Build-Information</name>
<manifestEntries>
<Project-Name>${env.JOB_NAME}</Project-Name>
<Build-Number>${env.BUILD_NUMBER}</Build-Number>
<SVN-Revision>${env.SVN_REVISION}</SVN-Revision>
</manifestEntries>
</manifestSection>
<manifestSection>
<name>Module-Information</name>
<manifestEntries>
<Group-ID>${project.groupId}</Group-ID>
<Artifact-ID>${project.artifactId}</Artifact-ID>
<Version>${project.version}</Version>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
<executions>
<execution>
<id>Client</id>
<configuration>
<finalName>Client</finalName>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>src/assembly/client.xml</descriptor>
</descriptors>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The <manifestSections> work just fine, but the <addMavenDescriptor> doesn't seem to be working although I've explicitly set it to true.
According to the documentation on the maven-archiver-plugin:
Whether the created archive will contain these two Maven files:
The pom file, located in the archive in META-INF/maven/${groupId}/${artifactId}/pom.xml
A pom.properties file, located in the archive in META-INF/maven/${groupId}/${artifactId}/pom.properties
The default value is true.
According the maven-assembly-plugin page:
<archive>
This is a set of instructions to the archive builder, especially for building .jar files. It enables you to specify a Manifest file for the jar, in addition to other options. See Maven Archiver Reference
Is there something simple I'm missing here?
The Maven Assembly Plugin actually ignores the addMavenDescriptor parameter, and will never include the Maven descriptor in the resulting assembly. This can be seen in the source code: only the META-INF/MANIFEST.MF file is possibly added to the JAR archive.
I couldn't find an existing JIRA issue about this, so I went ahead and created MASSEMBLY-835 to track this.
A work-around for now would be to add the files yourself in the assembly descriptor:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>client</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>pom.xml</source>
<outputDirectory>META-INF/maven/${project.groupId}/${project.artifactId}</outputDirectory>
</file>
<file>
<source>${project.build.directory}/maven-archiver/pom.properties</source>
<outputDirectory>META-INF/maven/${project.groupId}/${project.artifactId}</outputDirectory>
</file>
</files>
<!-- rest of your configuration -->
</assembly>
This adds a <files> configuration that adds the pom.xml and the generated pom.properties into the target directory.
Note that the pom.properties is generated by the Maven Archiver component during the default package goal into the target/maven-archiver directory; therefore, in order for it to be present when making your assembly, the Assembly Plugin has to be bound to the phase package (or later in the lifecycle), and the current Maven project needs to be of packaging JAR / WAR / EAR / EJB / RAR... but not POM which doesn't package an archive. The primary artifact of the Maven project also needs to be built (if you skip the generation of the primary JAR of a JAR project, the pom.properties won't be generated).
This works in a large majority of cases. But if you want a bullet-proof solution, you can just create the file yourself. Create a pom.properties somewhere in your project (example, base directory) with the following content:
#Generated by Apache Maven ${maven.version}
version=${project.version}
groupId=${project.groupId}
artifactId=${project.artifactId}
and in the previous assembly descriptor, have instead:
<file>
<source>pom.properties</source>
<outputDirectory>META-INF/maven/${project.groupId}/${project.artifactId}</outputDirectory>
<filtered>true</filtered>
</file>
This would correctly replace the placeholders inside the pom.properties that was created, and mimic what Maven Archiver would do.

spring boot loading jars (application dependencies and external file system jars)

I am trying to figure out what is the best way to setup a spring boot application in such a way that its has its own jar dependencies but additional jars are added to classpath at runtime when its being run as java -jar command. What approach makes more sense
Use the original jar (without dependencies added to it) and place all jars (application and runtime) in a folder on file system and use PropertiesLauncher to specify the loader.path to jars folder.
Use the fat jar (with application jars) place the additional jars on the filesystem and somehow include those as additional jars that need to be added to classpath. Not sure how this can be done.
Is there another better way to do this
The PropertiesLauncher was designed to work with fat jars, so you should be able to keep the fat jar and add as many additional dependencies as you like in an external location, e.g. with loader.path=/opt/app/lib:lib. I guess that's your option 2? If it doesn't work we can discuss in a github issue.
I resolved this issue using the following spring-boot-maven-plugin configuration, I had to build my Uber jar without excluded artifacts to create my external "lib" directory, then I added my excluded artifacts again and packaged my Uber jar with my application specific dependencies only.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.1.RELEASE</version>
<configuration>
<layout>ZIP</layout>
<executable>true</executable>
<excludeArtifactIds>
<!-- My libs which will be packaged with my Uber jar-->
<!-- core,data-feeder,engine,lightspeed-tcp-api,order-manager,store,strategies,utils,viewer -->
<!-- Other libs -->
antlr,aopalliance,aspectjrt,aspectjweaver,classmate,commons-lang,
dom4j,h2,hibernate-commons-annotations,hibernate-core,hibernate-entitymanager,
hibernate-jpa-2.1-api,hibernate-validator,jackson-annotations,jackson-core,jackson-databind,
jandex,javassist,javax.transaction-api,jboss-logging,jboss-logging-annotations,jcl-over-slf4j,
jul-to-slf4j,log4j-over-slf4j,logback-classic,logback-core,mysql-connector-java,slf4j-api,
snakeyaml,spring-aop,spring-aspects,spring-beans,spring-boot,spring-boot-autoconfigure,
spring-boot-starter,spring-boot-starter-aop,spring-boot-starter-data-jpa,spring-boot-starter-jdbc,
spring-boot-starter-logging,spring-boot-starter-tomcat,spring-boot-starter-web,
spring-boot-starter-websocket,spring-context,spring-core,spring-data-commons,spring-data-jpa,
spring-expression,spring-jdbc,spring-messaging,spring-orm,spring-tx,spring-web,spring-webmvc,
spring-websocket,tomcat-embed-core,tomcat-embed-el,tomcat-embed-logging-juli,tomcat-embed-websocket,
tomcat-jdbc,tomcat-juli,validation-api,xml-apis
</excludeArtifactIds>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Then, I added the following property to my "application.properties" which inside my jar "resources/" dir to specify my "lib" dir for Spring PropertiesLauncher where I put "lib" dir along with my jar in the same dir.
loader.path=lib/
Finally, I did run my jar using the following command
java -jar back-tester-0.0.1-beta-01.jar
Also, you can add the "loader.path" property to your command line without putting it in your "application.properties" like the following command but this way didn't work with me as I packaged my jar as an executable one which I'm running as linux service.
java -Dloader.path="lib/" -jar back-tester-0.0.1-beta-01.jar
Now, I successfully reduced my jar size from 29 M to only 1 M jar which contains only my application specific libs and it works out of the box.
thank you #Ashraf Sarhan, you rescue my two days :)
I added in pom file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<executable>true</executable>
<mainClass>vn.com.Mymainclass</mainClass>
<excludes>
<exclude>
<groupId>com.vn.groupId</groupId>
<artifactId>excluded-id-a</artifactId>
</exclude>
<exclude>
<groupId>com.vn.groupId</groupId>
<artifactId>excluded-id-b</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
And Placed ./lib folder containing two jars of two files which excluded above beside with my-main-spring-boot-app.jar file, and I ran:
java -Dloader.path="lib/" -jar my-main-spring-boot-app.jar
It worked perfectly.

How to get maven assembly plugin to copy artifact?

I have a Maven assembly script that copies resources to build our app. I need to copy some war files from separate, external projects into a /webapps directory in the output. Can't seem to find the magic commands to do it.
I tried adding a dependencySet to the assembly with <include com.mygroup:mywarfile>. This works if I add 'mywarfile' as a war dependency in the project with a scope of compile or runtime. Unfortunately, my project produces a war, and the maven-war-plugin includes the external mywarfile as an overlay, which I don't want.
If I set the scope of the external war dependency to provided or test, the assembly fails with the warning:
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
'com.mygroup:mywarfile'
All I want to do is have the assembly copy an artifact from my local repo to the assembly output. How to do it without messing up other parts of the project?
The maven-assembly-plugin is not intended for copying. The better way to copy dependencies is the maven-dependency-plugin which can copy dependencies etc. If you a talking about deployment into Tomcat etc. than you should take a deeper look into carg2-maven-plugin or the tomcat-maven-plugin which seemed to be more appropriate for that task.
I haven't tried this, but you could try using the exclude feature of overlay configuration of maven war plugin to exclude contents of the dependant war file from being included in your war project. Modified snippet from the Overlay documentation,
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>warToBeExcluded</artifactId>
<excludes>
<exclude>*</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...

How to include resources from war to another maven project

I have a maven project , which needs to copy webapp/WEB-INF/ resources from another maven project which is packaged as a war .
How do I do it ?
PLease suggest
As Bittrance said, you should use the maven dependency plugin.
The better way is to create project that include all your shared resources, probably a type zip, which is build up with the assembly plugin. This is the good "maven way". It's a better solution than unpacking a war.
Then, refer it
<dependency>
<groupId>com.mygroup/groupId>
<artifactId>my-dependencies</artifactId>
<version>1.0.0</version>
<type>zip</type>
</dependency>
Next, you use the maven dependency plugin to unpack your resources, in the directory of your choice (probably WEB-INF/ ?)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-cfg-test-resources</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>resources</phase>
<configuration>
<outputDirectory>${project.build.directory}/WEB-INF/</outputDirectory>
<includeArtifacIds>my-resources</includeArtifacIds>
<excludeTypes>pom</excludeTypes>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
I'm not realy sure of this code snippet (written for another purpose), but this is an example.
For more information, please follow this link : http://maven.apache.org/plugins/maven-dependency-plugin/
If you can't shared a common-project including your files, you can unpack war including only ftl (or whatever you want), but it's not a realy clean solution ;)
There is a lot of posts that deal with this subject :
Unzip dependency in maven
...
Just try with the keywords maven-dependency-plugin, unpack :)
Hope that will help you.
I can see some alternatives:
Use external references in your version control system to point all repos to the same files.
The Maven Dependency module can copy and unpack project dependencies. From there, you can use the Maven Assembly plugin (or Ant targets) to include parts of that dependency in your own installation.
At least for the FTL files, perhaps you could package them in a separate Jar file and then load them as resources through the class loader.
If the resources are filtered, you may get into problem with solution 1 if you want the filtered version and 2, 3 if you want the source version.
Hope this helps.
(This assumes your dependent project is java (jar) and not another web app, if it is a webapp I think the solution is similar).
I suggest a (slightly) different approach:
Instead of reading resources from war, add this to your war pom, to generate a jar in the artifact as well as a war:
<!-- maven war plugin config -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
...
<attachClasses>true</attachClasses>
<classesClassifier>some-string</classesClassifier>
</configuration>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
...
<resources>
<!-- This is for inclusion in the jar, so dependent module can load it -->
<resource>
<targetPath>some-path</targetPath>
<directory>src/main/webapp/path...</directory>
<includes>
<include>your-resource</include>
</includes>
</resource>
</resources>
And this to your consuming pom, so the generated jar will be loaded:
<dependency>
<groupId>com.company</groupId>
<artifactId>...</artifactId>
<classifier>some-string</classifier>
</dependency>
Then you will be able to load the resources the usual way (getResourceAsStream("some-path/your-resource"))

Exclude all the jar's from webapp/WEB-INF/lib

I have the third party jar's in my WEB project placed at /src/main/webapp/WEB-INF/lib/
I have mentioned the dependency for all the required JAR's in the pom.xml.
Now, Since the dependencies are defined in POM, the JAR's will be automatically packed in the lib folder.
I want to exclude all the JAR's in the lib.
The Dependency JAR's should be packaged inside the lib while building the WAR
I CANT DELETE THE LIB FROM WEB-INF BECAUSE ITS USED IN LOCAL DEVELOPMENT
This is what I've tried so far:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>META-INF/context.xml</packagingExcludes>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF/lib</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
Any Idea?
It should be "packagingExcludes" instead of "warSourceExcludes":
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
....
</configuration>
From http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html :
In version 2.1-alpha-1, this was incorrectly named warSourceExcludes
Try this:
<configuration>
<warSourceExcludes>WEB-INF/lib/*.jar</warSourceExcludes>
....
</configuration>
However, I do agree with Jarrod that it is bad practice to store your jars in WEB-INF/lib 'manually'. I used this before to avoid that jars coming as a dependency got packaged to be able to repackage it afterwards.
you shouldn't have anything in your WEB-INF/lib directory to begin with. If you are using Eclipse, you can tell it to build its project from the pom.xml and it will know to look in ~/.m2/repository for dependencies, Intellij IDEA does this as well. Putting dependencies in WEB-INF/lib kind of defeats the purpose of using Maven for dependency management. What happens when the dependencies in the pom.xml get out of sync version wise with those in WEB-INF/lib

Resources