Howto copy README.md into src/main/resources? - java

I want to show README.md file like help page in my web application. In order not to create duplicate, I need to copy by mvn from project path into resources.
How can I do so?
Any idea?
I have tried:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- this is important -->
<overwrite>true</overwrite>
<!-- target -->
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- source -->
<directory>/</directory>
<include>
<filter>**/README.md</filter>
</include>
</resource>
</resources>
</configuration>
</plugin>

The simplest solution would be to move the appropriate file(s) to src/main/resources folder whereas the second solution could be like this:
<build>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>README.md</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
No need for maven-resources-plugin to be configured this can be handled by the usual life cylcle and resource handling. The only thing you might need to adapt is the folder where the README.md is located. If you like having filtering you need to add the <filtering>true</filtering> part as well.
Copying something via Maven during the build into src/** is in general a bad idea, cause those folders are controled by version control systems which will result in uncommitted changes which you don't like to have.
Note: It would be wise to check for up-to-date versions of plugins (cause 2.3 is of 2008!). The list of the current plugin versions can be found here: http://maven.apache.org/plugins/

This works for me in one of my projects:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<!-- when to execute copy operation -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/pathTo.MD</directory>
</resource>
</resources>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I see that i have the phase and the goal extra from your version. I also used variables for the output location.

I would recommend you to be more equal to this example: http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html
With "more equal" I mean like using <executions> tag.
You can of course leave out things like <id> and filtering.
The following worked fine for me:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>myID</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src</directory>
<!-- Details about filtering: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html -->
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>

Related

Getting a logback.xml from a dependency jar and filtering it with the maven-war-plugin

We have a common logback.xml file that we would like to use across different web apps. It includes a RollinFileAppender which should see files named as the project artifactId. The logback.xml includes a property like so
<property name="LOG_FILE_NAME" value="$project.artifactId}"/>
Within our web project we would like to include a dependency e.g. logging-setup
<dependency>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
</dependency>
How do we easily allow the maven-war-plugin to filter this file so that the ${project.artifactId} reference is replaced with the actual project.artifactId? I think it can be done using a combination of the maven-dependency-plugin and the maven-war-plugin something like below. However that would need to be included in every project POM. Is there an easier way?
Thanks,
Paul
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
<version>${logging-setup.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<includes>**/logback.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</webResources>
<packagingExcludes>WEB-INF/lib/logging-setup-*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
After this logback.xml file is put inside an artifact jar file, it cannot be changed easily anymore during build time.
I would suggest a slightly different approach:
rename this file to e.g. logback-YOURCOMPANY.xml and change it so it can be included.
create a tiny logback.xml file put in each project as-is which sets the property as you do, and then includes logback-YOURCOMPANY.xml
enable filtering on this tiny logback.xml file so the variable is expanded during Maven build.
See https://logback.qos.ch/manual/configuration.html#fileInclusion for details.

Application.properties location when tomcat deploys

I have a war file with application.properties in default resources. I want to, when deploying on Tomcat, put these properties into an external folder like /webapps/example, not /webapps/namewar.
Can I configure my Maven or do something to achieve that?
Thank you so much.
You can use maven-resources-plugin's copy-resources goal to copy a file from your source tree to some directory. You need to add something like the following to your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- you may want to choose another phase -->
<!-- see https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- outputDirectory can be absolute or relative to ${basedir} -->
<outputDirectory>/webapps/example</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory><!-- or whereever your application.properties reside -->
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>

substring a variable in pom.xml

Is it possible (and how) to substring a variable in the pom.xml, or the properties that uses this variable?
My scenario:
I have a swing application that shows a kind of version in its footer.
This version is read from a properties file.
The properties file only have a reference for a maven variable, as:
version=${git.branch}
In my pom.xml, a have a plugin that looks for the branch name, and write it in the "git.branch" variable.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/version.properties</exclude>
</excludes>
</resource>
</resources>
</build>
But now we are using a prefix for the branch names, and this prefix shouldn't be deployed with the application version.
I had branches like:
v123
v124
v125
Now I have branches like:
b_04_v123
b_04_v124
And i want maven to get only the value "v124" of the string "b_04_v123", the prefix is fixed, aways like "b_NN_".
NOTE: No, it's not possible to change the branch names, as other departments uses them with scripts and automation.
you can use org.codehaus.groovy.maven plugin:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.setProperty("version2","${version}".replace('b_04_', ''))
</source>
</configuration>
</execution>
</executions>
</plugin>
and now you can use version2 :
version=${version2}
I solved it inside the java code, I already had a code reading the properties file, just added the substring there.
It isn't a beautiful maven solution, but worked. I could have done this since the beginning.
But, if someone could tell how can I substring a variable in my pom.xml it would be great, although I don't need it anymore (or with the same urgency) I'm still curious.
I stumbled upon this question myself and the other provided answers did not work for me. So I created my own Maven plugin.
It can search and replace other variables or normal text using regular expressions and so create a new variable.
For your case it would be something like this:
<plugin>
<groupId>io.github.1tchy</groupId>
<artifactId>variable-search-replace-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<text>${git.branch}</text>
<search>^b_\d\d_</search>
<variableName>old.version</variableName>
</configuration>
</execution>
</executions>
</plugin>
Then you can simply use it as a normal property: version=${old.version}
The plugin can also define a replacement text: check out its documentation!

Additional resources directories for images in Maven site

For a Maven site, the standard image directory is src/site/resources/images.
Unfortunately, my asciidoc editor copies images to src/site/asciidoc/images. Can I somehow add this directory to the site resources (as in the Maven resources plugin)?
To copy all resources to an outputDirectory, you can simply specify following in your pom.xml-
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/site/resources/images</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Sources - Example from the plugin itself.

Leave FXML files in /src/main/java

Whenever I put FXML files into the /src/main/java catalog, it seems like they aren't being included into the final OSGi jar during compilation. I suppose Maven removes them from there because it thinks that FXML files should only reside in the /src/main/resouces catalog. Is there a way to stop Maven from doing this (i.e. just leave them there)?
EDIT 1
The only solution I have found for now is:
<build>
<plugins>
....
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, as far I understand, there is another, shorter way of achieving this, i.e. without the use of additional plugins, in the <build>...</build> node. How do I use the shorter method instead?
Yes by default src/main/resources is for resource files.
You can use maven resources plugin to override this behavior.
Here's an example. In your pom.xml
...
<build>
<resources>
<resource>
<targetPath>com/company/projectname</targetPath>
<directory>src/main/java/com/company/projectname</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
where <directory> is the source package which includes resource files(in your case xml files) and <targetPath> is the target where you want the resources to reside. You can omit <targetPath> if you are ok with having these resources in the root path of the jar.

Categories

Resources