Maven maven-war-plugin not replacing values in web.xml - java

Maven version: 3.5.4
My web directory is not in the standard location. It is in /web
Maven War config
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<!--
Filter these files to look for ${my.maven.property} to replace them
at build time with a maven property value
-->
<resource>
<filtering>true</filtering>
<directory>web/WEB-INF</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>web</warSourceDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
<webXml>web/WEB-INF/web.xml</webXml>
<packagingExcludes>
${exclude.files.on.build}
</packagingExcludes>
</configuration>
</plugin>
properties snippet from pom.xml
<properties>
...
<!-- web.xml vars -->
<web.session.cookie.secure>true</web.session.cookie.secure> <!-- session cookie only sent over https -->
...
</properties>
web.xml snippet
<cookie-config>
...
<secure>${web.session.cookie.secure}</secure>
...
</cookie-config>
The property "${web.session.cookie.secure}" is not being replaced in the web.xml, and the property name is retained in the war file generated. I have not been able to pinpoint the configuration error. I am working in Intellij and get the same result whether I build the artifact off the intellij menu, or issue the mvn war:exploded command.
I am assuming that it may have something to do with the web directory location and a missing configuration item. The maven build runs as expected other than the issue with the properties not being replaced in the output.
Any ideas as to why the replacements would not be taking place using the filtering of the maven-war-plugin?

The maven-war-plugin uses ${basedir} as the location of the pom, so the target directory for filtering should be referenced via relative path from there.
<resource>
<filtering>true</filtering>
<directory>${basedir}/web/WEB-INF</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
The actuall path could be examined in mvn help:effective-pom.

Related

Maven filtering - replacement of variables

I have a maven project, which generates a jar file as a web project. Based on Maven I include a standalone Tomcat. Inside of the jar file, there is actually the war-file, which contains my application.
This application contains a "version.txt" in src/main/config (or any similar path), that is finally included in the war-file.
This version.txt looks like:
version: ${project.version}
I would like, that maven should replace the variable with the correct version from pom.xml.
In my pom.xml I have included:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
<filtering>true</filtering>
<includes>
<include>**/version.txt</include>
</includes>
</resource>
<resource>
<resources>
</build>
So is there any way to include this version.txt and a working replacement in a war-file, which is in a (Tomcat)jar-file?
Addendum:
My File hierarchy looks like:
jar-file
-- ...
--war-file
---- ...
----version.txt
I suggest you use the maven-war-plugin.
https://maven.apache.org/plugins/maven-war-plugin/usage.html
It will expect a certain directory layout, and in the examples it clearly shows how to filter (replace maven variables into the web resources)
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
If this solution falls short, then a more specific question based on this should should be asked later.
Thanks for the advice. No, there was no maven-war-plugin, but I have included it and based on the instructions, it works.
Short solution:
Added a resourceDirectory on same level like pom.xml and included version.txt
Added in pom.xml and activate filtering:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>externalresources</directory>
</resource>
</webResources>
</configuration>
</plugin>

Maven resources plugin doesn't filter files

I'm trying to build an rpm install with rpm-maven-plugin. In addition, I'm also trying to edit my post install script in order to use some Maven properties. For that reason, I'm using maven-resources plugin.
I'm following the answers in this post but it just doesn't work for me and the files aren't filtered and saved in the target directory.
My projects structure :
-my-app
-pom.xml
-app module
-src/..
-pom.xml
-rpm module
-pom.xml
-src/main/
-resources
-scripts
-post-install.sh
In the rpm module pom.xml I have the following two plugins :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/scripts/</directory>
<filtering>true</filtering>
<includes>
<include>post-install.sh</include>
</includes>
</resource>
</configuration>
</plugin>
and also :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
.......
<postinstallScriptlet>
<scriptFile>${basedir}/target/classes/post-install.sh</scriptFile>
<fileEncoding>utf-8</fileEncoding>
</postinstallScriptlet>
When I run mvn package I'm getting the following error :
[ERROR] Failed to execute goal org.codehaus.mojo:rpm-maven-plugin:2.2.0:rpm (default-rpm) on project my-app-package: Execution default-rpm of goal org.codehaus.mojo:rpm-maven-plugin:2.2.0:rpm failed: Invalid scriptlet declaration found - defined scriptFile does not exist: /root/my-app/rpm/target/classes/post-install.sh -> [Help 1]
I also tried to change the value of the include tag to **/post-install.sh but it didn't work.
To replace values in a file with values set in another file with maven this is a good way:
<!-- path to the final location of the resulting modified file - your output -->
<properties>
<filesPath>/Users/.../config/files</filesPath>
</properties>
<build>
<resources>
<resource>
<!-- the file to be modified. Here the name of the values to be changed in the properties file need to be added as variable names, e.g. ${varName} -->
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<!-- the file name or file types to be edited/updated -->
<include>*.xml</include>
</includes>
<!-- the target path is the location where the generated/edited resulting files should be saved. The default is `target/classes/` -->
<targetPath>${jdbcConfigFilesPath}</targetPath>
</resource>
</resources>
<filters>
<!-- the file where the variable values will be updated -->
<filter>file.properties</filter>
</filters>
</build>
This configuration will replace values in files from values set in a properties file.
If one wants to just set the values or the variables to be updated as properties in the pom.xml, one can just use the <properties> <yourVar>someValue</yourVar> </properties>option in the pom.xml instead of the
<filters>
<!-- the file where the variable values will be updated -->
<filter>file.properties</filter>
</filters>

Include different logback.xml in production build

I use Maven for building and would like to use logback-debugging.xml for developing but package another , logback-info.xml, to the final product.
I came up with the following Maven configuration:
<profile>
<id>development</id>
<activation>
<property>
<name>dev</name>
</property>
</activation>
<build>
<resources>
<resource>
<filtering>true</filtering><!-- if it is neccessary -->
<directory>src/main/logging/develop</directory><!-- from -->
<targetPath>${project.build.outputDirectory}</targetPath><!-- to -->
<includes><!-- what -->
<include>logback.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
In Maven settings file, we can activate one profile per default:
<settings>
[..]
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
[..]
</settings>
Another way is to move the default profile configuration to the "normal" pom build section.
To activate the development profile, pass the debug flag via command line, e.g. mvn package -Ddev
Logback searches for configuration files in a specific order (see the docs here). The first place it looks is for a file called logback-test.xml in your classpath. Since you're using maven, include that file in your test/resources directory. That way when you're running any of your tests, the logback-test.xml file is used. For your production code, include logback.xml in your main/resources directory.

How can I write maven build to add resources to classpath?

I am building a jar using maven with simple maven install.
If I add a file to src/main/resources it can be found on the classpath but it has a config folder where I want that file to go but moving it inside the config folder makes it disappear from the classpath.
A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.
For instance, place your property file in a new folder src/main/config, and add the following to your pom:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</build>
From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>my-config.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).
If you place anything in src/main/resources directory, then by default it will end up in your final *.jar. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:
*.jar is not correctly loaded (maybe typo in the path?)
you are not addressing the resource correctly, for instance: /src/main/resources/conf/settings.properties is seen on classpath as classpath:conf/settings.properties
By default maven does not include any files from "src/main/java".
You have two possible way to that.
put all your resource files (different than java files) to "src/main/resources" - this is highly recommended
Add to your pom (resource plugin):
​
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

Copy a file during maven build phase

My situation is:
I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
I have a javascript file in /common/script.js
Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.
I need this to share one version of resource files among many web-apps.
Kind regards,
Q.
You could do something like this, as documented here.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../common</directory>
<targetPath>/js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.
Check
"maven-resources-plugin"
You can use maven-resources plugin to copy a file to the desired location. Before or after a war has been built

Categories

Resources