Include a zip file in the Maven generated jar - java

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

Related

How to overwrite a jar file in a maven generated war?

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>

How do I include a "resources" directory alongside (not in with) my built JAR/classes in Maven?

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?

Using Maven resource plugin on dependecy jar file

Maven has resource plugin that can delimiter a character that can tell it in pom file , for example in the blow config define it that replace "#" character.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/filtered-resources/scripts</outputDirectory>
<resources>
<resource>
<directory>src/assemble/resources/scripts</directory>
<filtering>true</filtering>
</resource>
</resources>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</execution>
</executions>
</plugin>
but it work on files that directly exists in my project , i want to do the same job in a jar file , i have a jar file that exists some java script plugins and i want add it to my project but i need replace somethings on it .
and the dependency is something like this :
<dependency>
<groupId>sample.javascript</groupId>
<artifactId>jsLibrary/artifactId>
<version>1.0.0</version>
</dependency>
1) you need to unpack your jar. You can use this plugin: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html
2) change
<directory>src/assemble/resources/scripts</directory>
to the directory with unpacked jar

How to include source file(.java file) parallel to .class file in the generated jar using maven

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

maven-jar-plugin file filterring

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>

Categories