I want to replace a token #NAME# in a source file (in my case *.java) before compilation.
I try to use google replacer plugin but I am open for anything which will help me.
1.pom.xml
The pom file look like this
<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>src/main/java/com/test/sample/File.java</include>
</includes>
<replacements>
<replacement>
<token>#NAME#</token>
<value>New content</value>
</replacement>
</replacements>
</configuration>
</plugin>
But after I run mvn package the output is:
--- replacer:1.5.3:replace (default) # MyProject --- [INFO] Replacement run on 0 file.
Because there is no error I do not know what I have done wrong.
Maybe:
Defined phase is wrong
Defined include is wrong
...
Greetings!
I think there are two options.
If you keep using the plugin I think you need to add the ${basedir} to the include statement:
<include>${basedir}/src/main/java/com/test/sample/File.java</include>
If you dont want to modify the file in src/main but filter the file and add that one to the build you can use the standard resource filtering and the buildhelper plugin to add those "generated sources" to the build.
So step one would be using resource filtering to copy the file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
And then use the http://www.mojohaus.org/build-helper-maven-plugin/ to add those sources to the build.
Some IDEs (IntelliJ) will recognize /target/genereated-sources automatically if you keep using that folder (its not standard but very common). If you search for "maven" and "generated-sources" you will find quite some tutorials.
Hope this helps :)
While this is something you usually should not do in the first place, sometimes you have no choice (in my case it was "converting" an old project to Maven with changing as little of the code as possible). The above somehow did not work (while I could replace a placeholder in the source file and add the generated-sources folder to be compiled, it complained about duplicate source files).
Then I found an easier way by using the templating-maven-plugin as described here http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html:
Put the file with the placeholder in the folder /src/main/java-templates. Excerpt from my source code:
public static final String APPLICATION_VERSION = "r${project.version}";
Add the following to your pom's plugins section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>
Related
Trying to clean up my ${project.build.directory}, I noticed that maven-checkstyle-plugin puts three files into my target folder: checkstyle-checker.xml, checkstyle-cachefile and checkstyle-result.xml.
I was able to redirect the last two into their own folder target/checkstyle:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<cacheFile default-value="${project.build.directory}/checkstyle/checkstyle-cachefile"/>
<configLocation>google_checks.xml</configLocation>
[...]
<outputFile default-value="${project.build.directory}/checkstyle/checkstyle-result.xml">${checkstyle.output.file}</outputFile>
</configuration>
[...]
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
All I could find about checkstyle-checker is under propertiesLocation.
There is says:
If successfully resolved, the contents of the properties location is copied into the ${project.build.directory}/checkstyle-checker.properties file before being passed to Checkstyle for loading.
Can I change the location the contents of the properties location are copied into? Or is there another way to make the plugin write the checkstyle-checker.xml file into my custom folder target/checkstyle?
You cannot change the location of ${project.build.directory}/checkstyle-checker.properties without modifying/overwriting the plugin.
The documentation sounds clear regarding this point.
A gaze into the source code reveals, that checkstyle-checker.properties is hard coded. ... and the path (${project.build.directory}) is "deep in" maven/plexus.
Same applies to (the location of) checkstyle-checker.xml: DefaultCheckstyleExecutor.java#L723
I have a maven project, I need to generate three separate jars
one for the main application (default)
one for the source code (maven-source-plugin)
and one for my app documentation which reside in target/docs
Question: How can I create a jar containing only the target/docs folder?
You could add the following to your maven pom.xml file, build/plugins section:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<configuration>
<classifier>docs</classifier>
<classesDirectory>${project.build.directory}/docs</classesDirectory>
<includes>**/*</includes>
</configuration>
<id>pack-docs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Which is basically:
Using the maven-jar-plugin, jar goal to pack a new artefact for the project
Adding (or attaching) it as a classifier (docs suffix appended to the normal convention for project artefacts, e.g. artifactId-version.jar)
Setting the target\docs as source of files, using the standard ${project.build.directory} property instead of target (which is its value by default)
Running
mvn clean package
You would then have as part of the output
The jar file normally created, e.g. sample-project-1.0.0-SNAPSHOT.jar
The new docs file, e.g. sample-project-1.0.0-SNAPSHOT-docs.jar
I am trying to add an additional source folder to my current maven project by using build-helper-maven plugin.
This source folder contains some common classes, like utility classes.
For that, here is my relevant pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>C:/Users/CommonIncludes/src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Eclipse is showing the following error :
Build path entry is missing.
Project->Right Click->Java build path->Source->
Project/Users/CommonIncludes/src(missing)
Here the additional source location : "C:/Users/CommonIncludes/src" is outside of the workspace of the current project. But eclipse always treating this as a location from current project.
I am using Eclipse 4.3 and m2e.
How can I overcome this error through MAVEN, so that Eclipse can identify the linked source from correct location.? Or is there any alternate way to do this using MAVEN?
Any help will be greatly appreciated. Thanks in advance.
Found it in an alternate way..works great.!
Steps include
1. Removed build-helper-maven-plugin from pom .
2. Created another maven project and added the common classes in it. Added maven-source-plugin in this pom to generate sources.
3. To the same pom, added maven-dependency-plugin to copy this generated sources to the desired location (My project's src/main/java).
4. Run a maven build for common classes project.
Now the common code in my project. Thanks.
I had a lot of inconsistent trouble with this plugin.
by far the simplest workaround in my case was simply to apply the addition via the standard 'resources' options.
hope this helps someone.
<build>
<resources>
...
<resource>
<directory>${project.build.directory}/generated-sources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</build>
Can I use 'include' option (or something like this) to include any properties file to another properties file?
So,I have two properties files:
1. "firstPropertiesFile" which contains the next string:
include = secondPropertiesFile #it's path to second property file
and
"secondPropertiesFile" which contains the next string:
key = value
Also I have resource file (file which will be filtered by resources:resources goal) contains:
${key}
When I invoke resources:resources goal, I expect the next steps:
Resources plugin look into firstPropertiesFile file and see that it contains refer to another properties file.
The plugin go to reference (path to second property file) and see necessary key and gets value (in our case- value).
But this way doesn't work in maven. Could you hint me how to realize this?
P.S. This option is supported in Apache Commons Configuration: http://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html
("Includes" chapter).
There is nothing built in to standard Java Properties to do this, you need to code it if you want this or use a library which does this already.
In Maven, you can achieve something very similar Properties Maven Plugin.
I will not support the include semantics you are expecting, but it can compose a properties file from multiple sources. The example bellow will combine two properties files, which you can access from allprops.properties. This is especially useful when different sets of properties files must be used in different systems or environments.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>${properties-maven-plugin.version}</version>
<executions>
<execution>
<id>read-properties</id>
<phase>generate-resources</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>firstPropertiesFile.properties</file>
<file>secondPropertiesFile.properties</file>
</files>
</configuration>
</execution>
<execution>
<id>write-all-properties</id>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/allprops.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I thought that, if my project is named my-web-application, than when I call mvn clean install maven first compiles to target\my-web-application-1.0 folder and than "zips" that folder to my-web-application-1.0.war...
But as I found recently it's not how it (maybe just with default settings) works.
I'm using some 3rd party components and I'd like to modify some styles (.css) and behavior (.js) for those components, so I simply replace content in dependency jar with my modification using
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<plugin>
execution example follows (it zips org folder to componentToModify.jar)
<execution>
<id>fix</id>
<phase>package</phase>
<configuration>
<executable>jar</executable>
<workingDirectory>${basedir}</workingDirectory>
<arguments>
<argument>-uvf</argument>
<argument>target/${project.artifactId}-${project.version}/WEB-INF/lib/componentToModify.jar</argument>
<argument>org</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
what correctly modifies the componentToModify.jar in the target\my-web-application-1.0, but final war doesn't contain the modifications. It seems that libs are zipped to war from local repository.
Is there some reason for such behavior and can I change it somehow?
Not completely sure but would using an Overlay help here?
http://maven.apache.org/plugins/maven-war-plugin/overlays.html
If all else fails, you may just want to modify the 3rd party application and produce your own jar with your changes.