I has been copying some patched jars inside war files in order to test remediated jars without uploading them to the maven repo, and everything goes well when applying the maven-war-plugin as long as the jar does no exists they are placed inside the war, so no overwrite is happening, but the copy does happen.
Those jar files belong to several dependencies which I have no control whatsoever and could be any number of files, also I can't replace them in the nexus before testing nor place them in my local repo, since the idea is to generate the war file in other machines using jenkins to test the jars.
So, how can I overwrite the jar files that are packaged into the war file?
This is my previous configuration that allows me to copy any number of jars from a libs folder to the WEB-INF/lib inside the war file.
<!-- OVERWRITE JAR files with remediated versions -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>default-war</id>
<phase>none</phase>
</execution>
<execution>
<id>war-replace</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<webResources>
<resource>
<directory>${basedir}/libs</directory>
<includes>
<include>**/*.jar</include>
<include>**/*.war</include>
</includes>
<targetPath>WEB-INF/lib/</targetPath>
</resource>
</webResources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
Related
I am working on a maven project with a war external dependency (let's call this war dependency WAR-DEP)
After the build and during the package phase I am taking the content of WAR-DEP and merging it with the content of the current build using the overlay feature of the maven-war plugin.
In WAR-DEP we have some required jars in it's WEB-INF/lib folder so with the overlay we end up getting everything we need in our final war but our problem started when the project providing us with the WAR-DEP war added a new folder in the WEB-INF/lib-new and moved some of the jars we had before in the WEB-INF/lib folder to this new folder WEB-INF/lib-new.
After building with this new version of the WAR-DEP the overlay worked as expected so we ended up having two folders in the WEB-INF (lib and lib-new) and our application stopped working since this WEB-INF/lib-new is not recognized by tomcat server. So without changing the classpath on tomcat side is there a way I can move the content of lib-new into the lib folder before generating the war ? I mean for example during the overlay but I am not sure how to do this.
Thanks for your inputs.
maven-war-plugin does not have required functionality, however maven-dependency-plugin may help, smth. like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>unpack-lib-new</id>
<goals>
<goal>unpack</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>dep-group-id</groupId>
<artifactId>dep-artifiact-id</artifactId>
<version>dep-version</version>
<type>war</type>
<outputDirectory>${project.build.directory}/${build.finalName}/WEB-INF/lib</outputDirectory>
<includes>WEB-INF/lib-new/*</includes>
<fileMappers>
<org.codehaus.plexus.components.io.filemappers.FlattenFileMapper/>
</fileMappers>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
...
<overlays>
<!-- current project -->
<overlay/>
<overlay>
<id>dep-skip-lib-new</id>
<groupId>dep-group-id</groupId>
<artifactId>dep-artifact-id</artifactId>
<excludes>
<exclude>WEB-INF/lib-new/*</exclude>
</excludes>
</overlay>
</overlays>
...
</configuration>
</plugin>
I need to include a zip file in the src/test/resource dir in the maven generated jar .
Any idea how to do that?
As suggested in isapir's answer, you should ideally place the zip file under src/main/resource if that needs to be included in your jar file. But if you really need to include it from your src/test/resource folder, you can use build-helper-maven-plugin as below:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<include>ABC.zip</include>
<directory>src/test/resource</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You should put the zip archive in the src/main/resources directory and not in src/test/resource.
If you want to have more control then check out the Maven Resources Plugin documetation: https://maven.apache.org/plugins/maven-resources-plugin/index.html
I'm using Maven in IntelliJ and I want to include a 'resources' directory in my build output alongside the JAR and/or in whatever directory IntelliJ uses as the cwd when running my project. I don't want these files in the JAR/Classpath, just alongside the final built project.
The idea is that then, my project can refer to files named things in src/ext-resources/foo.txt as resources/foo.txt.
I'm currently using the following instruction in pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/ext-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, this doesn't appear to actually make it accessible, and doesn't seem to be copying these files at all. At one point, my config resulted in the subdirectories of src/ext-resources being copied into target/classes for some reason, but they aren't being copied into target/resources no matter what.
How do I make maven copy this folder into my final output directory?
I wanted to create my jars with wars with that should include source file(.java file) parallel to .class file in the generated jar using maven. (I know there are some plugins available to generate a separate xyy-sources.jar file. But I dont want to create a seperate source jar. I need a single jar file with both .class and .java file exists parallel)
You only have to add resources under build tag.
For example.
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
</build>
Now every jar and war file you creat would contain .java files also. :)
I'm still not sure I understand why you would want to do this, but you could use the maven-resource-plugin and specifically its copy-resource goal to copy your java files to any location within your build directory. So for example to copy them to the classes folder which then gets included in the jar that is built automatically you could do the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/src-files-location</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I am not sure if including .java adjacent to .class is a very good idea. Any specific reason you want to do that?
If you need to include sources, you can create a separate source jar (which is not you asked for, but is recommended way) like this by executing mvn package
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Does this help? http://maven.apache.org/plugin-developers/cookbook/attach-source-javadoc-artifacts.html
I have a some perl file in my src/main/java/com/pac/report.pl which I want to package as part of my classes in the jar file.
Using maven maven-jar-plugin include directives, I have tried below and various other suggestions I pulled off the web, but doesn't copy the perl file as part of my classes in the jar file. Does anyone know what I am doing wrong.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
EDIT
Also let me point out that I don't want to place the file in the resource directory due to legacy call and dependent reasons.
That is because the classes packaged into your jar aren't taken from src, but rather from target (specifically /target/classes), and the compiler completely ignores your non-java file.
Try placing your file in src/main/resources/com/pac/report.pl and it should be packaged into the jar (with the relative path of /com/pac/report.pl) since thats the default location where the resources plugin looks for additional files to add to /target before the jar plugin runs.
EDIT - or, if you dont want to / cant do this the way maven expects, you could manually bind an execution of the resources plugin to the lifecycle to pick up your file and copy it over to target. something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase> <!-- or later -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- path to your *.pl file here -->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>