I have a text in a java constant that I want to be replaced according to a maven variable that is configured when generating the artifacts in the following way:
public class FOO {
public static final String BASE = "/#FOO#";
}
The problem is that if I replace the java code, it is replaced forever and the replacement is no longer performed, so if I change the value of the variable it has no effect:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
I have fixed this by doing the process in reverse:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>first-execution</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</execution>
<execution>
<id>second-execution</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>${my.custom.property}</token>
<value>#FOO#</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>
But this second step can be dangerous, as there can be conflicts and replace something that has the same value in the java code of the class.
Another alternative would be to replace in the .class files as follows:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/target/my-artifact-directory/WEB-INF/classes/com/my/package//Constants$PATHS.class</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
The replacement works but the application does not start correctly.
Any other ideas on how to perform the replacement without modifying the original code?
I'd create a resource file in src/main/resources that is read in FOO and use the Maven Resources Plugin's Resource Filtering similar to the answer to spring maven profile - set properties file based on compilation profile:
foo.properties
BASE=${propertyName}
pom.xml
<project>
...
<properties>
<propertyName>property value</propertyName>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
...
I would like to generate a jar containing package 'com.x' and a class (let's say, Utils.class) from another package 'com.y'. I am able to bundle the complete package com.y in the shaded jar but I only want the one class. It doesn't seem to work with I have below. I have also tried by providing the path to the class: com/bar/cli/pol/Utils.class without any luck.
<dependencies>
<dependency>
<groupId>com.x</groupId>
<artifactId>foo</artifactId>
</dependency>
<dependency>
<groupId>com.y</groupId>
<artifactId>bar</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.x:foo</include>
<include>com.y:bar:**/Utils.class</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I got this working by doing the following:
<configuration>
<finalName>myshadedjar</finalName>
<artifactSet>
<includes>
<include>com.x:foo</include>
<include>com.y:bar</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>com.y:bar</artifact>
<includes>
<include>com/bar/cli/pol/Utils.class</include>
</includes>
</filter>
</filters>
I am trying to create single fat jar using maven assembly plugin but somehow after running maven clean install it give 2 jars, one is client-1.0-SNAPSHOT.jar and other one is client-1.0-SNAPSHOT-jar-with-dependencies.jar.
I am only interested to create a jar with dependencies so dont know why the other jar is also creating by this assemble plugin.
Can someone tell me how to eliminate this jar Or should i use some other maven command rather than clean install ?
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
-->
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.app.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- overwrite! -->
<overwrite>true</overwrite>
<outputDirectory>${project.basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
i need to change multiple xml file while build a war using maven like below example
I am able to do the required change in the target folder. But It is not copied in to the war file
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<id>dse xml replacer</id>
<configuration>
<file>
${project.artifactId}/target/${project.artifactId}-${version}/WEB-INF/example.xml
</file>
<replacements>
<replacement>
<token>reloadingEnabled=".*"</token>
<value>reloadingEnabled="false"</value>
</replacement>
</replacements>
</configuration>
</execution>
</plugin>
Try the maven-war-plugin, like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
More info https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
The Maven resources plugin:
This goal requires that you configure the resources to be copied, and specify the outputDirectory.
Copy two (or more) external resource directories within the basedir to the build output directory using maven (see blah and uggh).
${basedir}/
- pom.xml
- blah/
- uggh/
- src/
- main/..
- test/..
- target/
- classes/..
- blah/
- uggh/
For example, given the directory structure above copy blah and uggh to the target directory using maven. It is easy to copy one or the other, however, the plugin only accepts a single outputDirectory. If you specify the target directory and both directories as resources, then the contents of each directory gets copied to target but not the directories themselves.
Additional use of the plugin overwrites the initial. Also, I've tried specifying the entire basedir and only including the desired directories. This does not copy anything.
Here is an example of copying a single directory:
<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/blah</outputDirectory>
<resources>
<resource>
<directory>blah</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This is where the file ends up:
<outputDirectory>${basedir}/target/blah</outputDirectory>
This is where it is copied from:
<directory>src/main/otherresources</directory>
There would be an <include> or <includes> tag to tell the file name(s)
Multiples
You need multiple <execution>s with different <id>s for multiple folders:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources-1</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/blah</outputDirectory>
<resources>
<resource>
<directory>blah</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-2</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/ughh</outputDirectory>
<resources>
<resource>
<directory>ughh</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
For me this one works well in Maven 3:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>custom-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<targetPath>${basedir}/target/blah</targetPath>
<directory>blah</directory>
<filtering>true</filtering>
</resource>
<resource>
<targetPath>${basedir}/target/uggh</targetPath>
<directory>uggh</directory>
<filtering>false</filtering>
</resource>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
</plugin>
This is the simpler solution I've found and it's working...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}/src/main/java/org/mc2/mymusic/gui/main/Menu/resources</directory>
<targetPath>${basedir}/target/classes/org/mc2/mymusic/gui/main/Menu/resources</targetPath>
<filtering>false</filtering>
</resource>
</resources>
</build>
You can use ant-style patterns
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>blah/**</include>
<include>uggh/**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<resources>
<resource>
<directory>${basedir}/src/scripts</directory>
<includes>
<include>data-octopus.ps1</include>
</includes>
<targetPath>${basedir}/target/data</targetPath>
</resource>
<resource>
<directory>${basedir}/src/scripts</directory>
<includes>
<include>service-octopus.ps1</include>
</includes>
<targetPath>${basedir}/target/service</targetPath>
</resource>
</resources>
</plugins>
...
</plugins>
Reading your example I don't think you have to include&configure the maven-resource-plugin.
Just add those resource-elements to the <build><resources/>-tag. See http://maven.apache.org/ref/3.1.1/maven-model/maven.html#class_resource which other tags you can use.
Maven hides everything to make it easier to code.
There are several ways you can achieve this.
Edit the default execution in Resources plugin. (Easiest)
This also can be written using include tag or different resources
Write different executions in Resources plugin.
Use Antrun plugin. (You might as well write the whole build in ant)
Maven Copy-rename plugin.
And many other ways that I am not mentioning here....
Edit the default plugin--
<resources>
<resource>
<directory>${basedir}<directory>
<includes>
<include>blah</include>
<include>ughh</include>
</includes>
<resource>
<resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
</configuration>
</plugin>
</plugins>
If you want to copy more directories or files - a better option:
<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>
<tasks>
<copy todir="${basedir}/target/blah" overwrite="true">
<fileset dir="blah"/>
</copy>
<copy file="${basedir}/target/blah/somefile"
todir="../target_webapp_eclaims/WEB-INF" overwrite="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<targetPath>${basedir}/target</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<targetPath>${basedir}/target/classes</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jks</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<executions>
<execution>
<id>copy-resources-1</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
</plugins>