maven-jar-plugin file filterring - java

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>

Related

Include a zip file in the Maven generated jar

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

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?

Maven Copy Folder Before War File Generation

I have two Git repositories. One repositary is java services (maven web project) and another repository consists of UI {HTML, JS, CSS}(non maven), At the time of java services repository build I want to include the latest UI (master) into the war file. I tried with maven-resources-plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/</outputDirectory>
<resources>
<resource>
<directory>/home/srinivas/AAA/bb/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
mvn install
It copies the resources to target folder but it is not placing them in the war file
You are using the wrong phase in your execution as the package phase is actually the phase where your war is created, so you need to execute at an earlier phase e.g. prepare-package.
You should definitely read Introduction to the Build Lifecycle for clarification.
In addition you should not become accustomed to pulling in resources via maven-resources-plugin from the file system. This is generally frowned upon as bad practice since other developers will not be able to reproduce your build.
Using a repository manager to store your dependencies is the way to go here. Read Why do I need a Repository Manager? to get started.
at first glance, change :
<phase>install</phase>
to
<phase>prepare-package</phase>

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

Rename a generated file in Maven before building WAR

Fingers crossed you can help me!
I am using SmartSprites to combine the PNGs on my landing page into one, so that it will load quicker.
SmartSprite will examine your CSS files, generate a CSS Sprite image, and create a new CSS file which will use this sprite image instead of the originals. What I want to do is replace the original CSS file with the SmartSprite one automatically during my maven WAR build.
So here's what I would like to happen:
SmartSprite scans my CSS file: mystyle.css
SmartSprite create a sprite image, and creates a new mystyle-sprite.css file, which references the new sprite image.
I want to copy mystyle-sprite.css over mystyle.css before the WAR is built, so that I don't have to change any of my references in the JSP files.
Both files are located in the output directory (target/myproj/css). There doesn't seem to be any flag in SmartSprite to override the original file, so I guess it had to be done post-processing.
Below is the maven plugin configuration I'm using for SmartSprite.
<plugin>
<groupId>org.carrot2.labs</groupId>
<artifactId>smartsprites-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>spritify</goal>
</goals>
</execution>
</executions>
</plugin>
I'm afraid you won't find anything simpler or more elegant than Maven AntRun Plugin with something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<copy file="${project.build.directory}/mystyle-sprite.css"
tofile="${project.build.directory}/mystyle.css" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can use the Maven WAR plugin:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory><!-- Your output directory --></directory>
<targetPath><!-- The target location of the files below --></targetPath>
<includes>
<include><!-- file pattern --></include>
<include><!-- file pattern --></include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
You should also configure SmartSprites to use a different output directory to preserve the original CSS filename. Try the output-dir-path option with an empty css-file-suffix value.

Categories