I have in my Web.xml a value
<display-name> MyApp version #minorversion#- Pré prod </display-name>
I want to know if It’s possible to read a properties file with maven to replace the value in my Web.xml when I want to compile or package..?
Add src\main\resources\conf.properties:
minorversion=1.0.0.0
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<resourceEncoding>${project.build.sourceEncoding}</resourceEncoding>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<filters>
<filter>src/main/resources/conf.properties</filter>
</filters>
</configuration>
</plugin>
web.xml
...
#minorversion#
or
${minorversion}
...
You can use the maven-replacer-plugin
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>REPLACE</id>
<goals>
<goal>replace</goal>
</goals>
<phase>{Phase to execute in}</phase>
<configuration>
<file>${Path to file to replace}</file>
<regex>true</regex>
<token>${Regex to match in file}</token>
<value>${New Value to Replace matched}</value>
<regexFlags>
<regexFlag>MULTILINE</regexFlag>
<regexFlag>DOTALL</regexFlag>
</regexFlags>
</configuration>
</execution>
</executions>
</plugin>
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
I am trying to add some filtering to the Spring application context file, which resides in src/main/resources folder but it's not working. I put my filter file in src/main/filters
I've tried many solutions but none are working when i launch unit test through maven install or junit but if i skip test it's filtering it's working .
I've modified the file .classPath , I removed exclude attribute from the file
edit classpath solution then I read this article bug in maven-resources-plug who said that there is a bug in maven-resources-plugin so I updated the plugin to a newer version but it's still not working.
My pom.xml :
<build>
<finalName>core-impl</finalName>
<directory>target</directory>
<sourceDirectory>src/main/java</sourceDirectory>
<outputDirectory>target/classes</outputDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
</build>
i tried this solution and it's working .
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/src/main/filters/filter.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/src/test/filters/filter.properties</filter>
</filters>
</configuration>
</execution>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
</testResource>
</testResources>
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>
Hey, I'm talking about profiles within pom.xml of a project. Could please anybody explain to me, why if I have 2 profiles in pom definition and I run test phase from one of the profiles, both the Main method is executed and all the tests are run by surefire plugin ? I mean, even the surefire plugin runs all the tests, even though it is within a different profile ?
mvn test -Pcode-generator
the first one,code-generator, is just for Main methods execution and the second one for the rest of the project.
<profiles>
<profile>
<id>code-generator</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>cz.instance.transl.Main</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>default</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warName>${war.file.name}</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<useFile>false</useFile>
<argLine>-Dportal.test=generic
-Dwebdriver.chrome.driver="/opt/google/chrome/chromedriver"
-Dwebdriver.development=true
-Dwebdriver.firefox.useExisting=true
-Dwebdriver.firefox.profile=webdriver
-Dwebdriver.reap_profile=true
-Dsurefire.useFile=false
-Xmx2048M
-XX:MaxPermSize=1048M
-XX:+CMSClassUnloadingEnabled
</argLine>
<skipTests>false</skipTests>
<suiteXmlFiles>
<suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>META-INF/log4j.xml</value>
</property>
</systemProperties>
<includes>
<include>cz/instance/transl/tests/selenium/*Test.java</include>
</includes>
<excludes>
<exclude>cz/instance/transl/tests/sample/*Test.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.7.1</version>
<!-- <configuration> <useFile>false</useFile> </configuration> -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<includes>
<include>**/*</include>
</includes>
</testResource>
</testResources>
<resources>
<resource>
<directory>${project.basedir}/src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>service.properties</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>profiles/*</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
The surefire plugin is one that runs by default for a java project. You have a number of options:
Add the surefire plugin to your code-generator profile and override it not to run.
Run with -DskipTests.
Specify only the goal you want to run, instead of running a full maven build.