How to include/exclude resources in maven war package - maven-3

Within my MAVEN project I'm trying to build a war package with certain resources based on a profile (defined in my settings.xml).
pom.xml:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- archiveClasses>true</archiveClasses Enable this line will remove compiled classes from package -->
<!-- packagingExcludes>view/test/**,WEB-INF/classes/**</packagingExcludes Does not work -->
<packagingExcludes>view/test/**</packagingExcludes>
<webResources>
<resource>
<directory>src/main/webapp/</directory>
<filtering>false</filtering>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>resources/</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>false</filtering>
<!-- excludes><exclude>**</exclude></excludes Does not work -->
<includes>
<include>ehcache.xml</include>
<include>${include.files}</include>
</includes>
</resource>
</webResources>
<includeEmptyDirectories>true</includeEmptyDirectories>
</configuration>
</plugin>
My src/main/resources folder is empty, so in my package there are no config files that are not expected. But when I move my resources folder into src/main/resources then the profiles are not working anymore and the package always contains all files from the resources folder.
How to alter my pom.xml so that resources folder can be moved into src/main/resources as to my understanding that is where you store resources like configuration files etc ('best-practice')?

See if you can reorganize your resources in a way so that you can apply a filter to the resources and that most files are always present for every configuration.
Suppose you have a properties file that need different values for different configurations. You can substitute the value with a variable like this:
url=${url}
mode=${mode}
Now you can use profiles to set the values for the files that need to be filtered and the ones that need to be excluded entirely for that configuration:
<profile>
<id>production</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>[non-resource file #1]</exclude>
<exclude>[non-resource file #2]</exclude>
<exclude>[non-resource file #3]</exclude>
</excludes>
</resource>
</resources>
</build>
<properties>
<url>www.something.com</url>
<mode>production</mode>
</properties>
</profile>
<profile>
<id>development</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>[non-resource file #1]</exclude>
<exclude>[non-resource file #2]</exclude>
<exclude>[non-resource file #3]</exclude>
</excludes>
</resource>
</resources>
</build>
<properties>
<url>localhost:8080/something</url>
<mode>development</mode>
</properties>
</profile>
Finally you can delete the webResources tag in your maven-war-plugin. It wil pick up src/main/resources now because it's a configured resource.

Related

Order of resource directories in Maven

I would like to have my Java project using Maven set up so that when developing locally and when running unit tests, resources are taken from the standard src/main/resources and src/test/resources directories, while when applying a profile the resources are overwritten with deployment-specific ones. Ideally, the profile specific resources will be a subset of the full resources, so the process should work by first writing the regular resources, then applying the profile specific ones on top of the first.
My directory layout is the standard one, with the profiles resources under src/main/env/{profile}.
I managed to do this using multiple stanzas under build/resources:
<profiles>
<profile>
<id>dev</id>
<activation><activeByDefault>true</activeByDefault></activation>
<properties>
<targetEnv>dev</targetEnv>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<targetEnv>test</targetEnv>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<targetEnv>prod</targetEnv>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/env/${targetEnv}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<build>
This is working, however, my first attempt was with the two resource directories ordered in the opposite way (first the regular directory, the the profile specific one), in a way to match the overwriting process. Instead, it was overwriting the profile specific files with the regular ones.
Does Maven guarantee an order for the stanzas under build/resources at all?

filter maven properties using profile-dependent properties file

I'm using the properties-maven-plugin to load properties from a custom properties file. The properties file name depends on the selected build profile.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/vpn/vpn-${environment}.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Each vpn-${environment}.properties file contains two properties, e.g. for dev environment:
vpn.username=dev_user
vpn.password=dev_password
Two profiles default and dev have been defined:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>default</environment>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
</profile>
</profiles>
What I'm trying to do now is to use the maven-resources-plugin to replace the vpn property placeholders used within a unix template file with the property values of the corresponding active profile and to copy this file to another location.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions> <execution>
<id>copy-script</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/vpn/bin</outputDirectory>
<resources>
<resource>
<directory>${basedir}/unix-template</directory>
<filtering>true</filtering>
<includes>
<include>customUnixTemplate</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
The customUnixTemplate file contains something like vpn-connect ${vpn.username} ${vpn.password}.
After I build the project with mvn clean install -Pdev and open the output file in the src/main/vpn/bin directory I can see for a split second that the properties were taken from the correct properties file, i.e. vpn-dev.properties, but the file gets overwritten by the default properties file and I can't figure out why. I suspect that it has something to do with the build phases/orders in which the plugins are executed. Can anybody help?
Edit in response to Andrei's comment:
The maven resource plugin uses a second execution:
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>log4j2.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>

Fortify + maven exclude xsd files

I have an issue with running scan with excluding xsd files in fortify SCA.
I am using maven (with fortify plugin) + jenkins.
My POM.xml used by Jenkins:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.bluecode</groupId>
<artifactId>bc</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
</properties>
<scm>
<connection>scm:svn:https://subversion......</connection>
<developerConnection>scm:svn:https://subversion.....</developerConnection>
<tag>HEAD</tag>
<url>https://subversion......</url>
</scm>
<profiles>
<profile>
<id>common</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!skipCommonProfile</name>
</property>
</activation>
<modules>
<module>Project1</module>
<module>Project2</module>
</modules>
</profile>
<profile>
<id>profile1</id>
<modules>
<module>Project3</module>
<module>Project4</module>
</modules>
</profile>
</profiles>
<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>${basedir}/src</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src</directory>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.fortify.ps.maven.plugin</groupId>
<artifactId>maven-sca-plugin</artifactId>
<version>2.6</version>
<configuration>
<source>1.5</source>
<failOnSCAError>true</failOnSCAError>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
My Jenkins Maven goals looks like:
1st goal:
com.fortify.ps.maven.plugin:maven-sca-plugin:2.6:clean -Pprofile1
2nd goal:
com.fortify.ps.maven.plugin:maven-sca-plugin:2.6:translate -Pprofile1
3rd goal:
com.fortify.ps.maven.plugin:maven-sca-plugin:2.6:scan -Pprofile1
Unfortunately I can not attach image.
Above configuration is working as expected till now.
Now, I'd like to exclude all XSD files from scan.
How to do it?
I tried to add -exclude ".xsd" entry in maven goals:
com.fortify.ps.maven.plugin:maven-sca-plugin:2.6:scan -Pprofile -exclude ".xsd"
but it doesn't work.
If someone can help we I would be grateful.
Thanks.
in theory the Fortify Maven Plugin supports the exclusion of files, but it doesn't always work as expected.
Now, that being said, you are not invoking the exclusion correctly. Where you built the Fortify Maven Plugin, find the documentation for the translate goal, e.g. for me: /Samples/advanced/maven-plugin/target/site/translate-mojo.html#exclude.
There you can find the correct way to invoke exclusion. On the command line:
"-Dfortify.sca.exclude=*.xsd"
or in the POM (if you set up the fortify translate job there)
<exclude>
*.xsd
</exclude>
Now back to my first point. Sometimes exclusion is more difficult to effect than just by setting the value as *.extension. You may also need to specify the directory as well, so if the above doesn't work, try also the Fortify special glob parameter "**" which means any recursive subdirectory match. To wit:
"-Dfortify.sca.exclude=**/*.xsd"
or
<exclude>
**/*.xsd
</exclude>
If neither of the above work, then contact Fortify Technical Support.
Pro tip: you can also set this value in Core/config/fortify-sca.properties, where it will affect every invocation of sourceanalyzer on the system. That includes invocations via the maven plugin.

How to override Maven 3.0 parent profile properties from child pom?

How to override Maven 3.0 parent profile properties from child pom?
I want to be override profile properties from a parent pom. I've used help:effective-pom -Pjmeter and can see that the child properties are not being picked up and tried many various permutations all without success. I expect the parents properties to be overridden by the child properties.
Parent pom profile:
<profile>
<id>jmeter</id>
<properties combine.self="override">
<maven.jmeter.phase>verify</maven.jmeter.phase>
<maven.jmeter.goal>jmeter</maven.jmeter.goal>
<!-- We use the ${basedir} to avoid NullPointer errors when src/test/jmeter doesn't exist -->
<!-- when running jmeter test the default to set in the child pom is ${basedir}/src/test/jmeter -->
<maven.jmeter.testFilesDirectory>${basedir}</maven.jmeter.testFilesDirectory>
<maven.jmeter.jMeterTestFile>**/*.jmx</maven.jmeter.jMeterTestFile>
<maven.jmeter.excludeJmeterTestFile>NOT_NULL</maven.jmeter.excludeJmeterTestFile>
<maven.jmeter.testResultsTimestamp>false</maven.jmeter.testResultsTimestamp>
<maven.jmeter.server>localhost</maven.jmeter.server>
<maven.jmeter.port>8080</maven.jmeter.port>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>${jmeter.version}</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>${maven.jmeter.phase}</phase>
<goals>
<goal>${maven.jmeter.goal}</goal>
</goals>
<configuration>
<testFilesDirectory>${maven.jmeter.testFilesDirectory}</testFilesDirectory>
<testFilesIncluded>
<jMeterTestFile>${maven.jmeter.jMeterTestFile}</jMeterTestFile>
</testFilesIncluded>
<testFilesExcluded>
<excludeJmeterTestFile>${maven.jmeter.excludeJmeterTestFile}</excludeJmeterTestFile>
</testFilesExcluded>
<testResultsTimestamp>${maven.jmeter.testResultsTimestamp}</testResultsTimestamp>
<propertiesUser>
<!-- server and port must be defined (and used) in the JMeter jmx file as User Defined Variables
${__P(server,localhost)}
${__P(port,80)}
See http://jmeter.apache.org/usermanual/component_reference.html#User_Defined_Variables
http://jmeter.apache.org/usermanual/test_plan.html#using_variables
http://jmeter.apache.org/usermanual/functions.html#__P
http://jmeter.apache.org/usermanual/best-practices.html#parameterising_tests
-->
<server>${maven.jmeter.server}</server>
<port>${maven.jmeter.port}</port>
</propertiesUser>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
and then in the child pom:
<profile>
<id>jmeter</id>
<activation><activeByDefault>false</activeByDefault></activation>
<properties>
<maven.jmeter.testFilesDirectory>${project.basedir}/src/test/jmeter</maven.jmeter.testFilesDirectory>
<!-- csv based JMeter tests result in one graph in Jenkins, we want a graph per test -->
<maven.jmeter.excludeJmeterTestFile>**/KRAD.jmx</maven.jmeter.excludeJmeterTestFile>
</properties>
</profile>
I'm not sure properties are overridden that way. Either way, the plugin will not run as you have it, since you only define <pluginManagement> and no straight direct <plugins> child under <build>. If you don't want your plugin to run in the parent, just define the <plugins> tags in the children where you do want this running as such:
<profile>
<id>jmeter</id>
<activation><activeByDefault>false</activeByDefault></activation>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<configuration>
<testFilesDirectory>${maven.jmeter.jMeterTestFile}</testFilesDirectory>
<!-- csv based JMeter tests result in one graph in Jenkins, we want a graph per test -->
<testFilesExcluded>
<excludeJmeterTestFile>${maven.jmeter.excludeJmeterTestFile}</excludeJmeterTestFile>
</testFilesExcluded>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Hope this helps.

Trying to get Sonar CXX Plugin working

Setup Jenkins (with sonar plugin) and sonar (with cxx-plugin). There is a build step to create the "......./gcovr-reports/gcovr-result-test.xml" file. Problem is coverage results to not show up ob Snar dashboard. But it appears that sonar.runner is excuting .... but transcript does not show that CxxGcovrSensor is being run.
what errormessages do you get?
try importing your project using maven for more details on errormessges.
I had similar issues because my pom.xml was broken. below is a working one.
be sure your folder structure looks like this:
(you need to cd to base)
base=/some/folder/myProject/ <-- put pom.xml here
src_folder=/some/folder/myProject/src
reports_folder=/some/folder/myProject/reports (with all the subdirs for reports)
also be sure your reports are named lik: xunit-result-.xml
cd to /some/folder/myProject and mvn sonar:sonar
(mvn -X sonar:sonar for debug mode)
(btw. if you get strange errors try removing all irrelevant plugins from sonar/external/plugins (just leave cxx there)
do not forget to set JAVA_PATH for sonar
goto basefolder
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>NETICOA</groupId>
<artifactId>MYPROJECT</artifactId>
<version>5.3.10-myversion</version>
<packaging>pom</packaging>
<name>myProject</name>
<description>php svn code</description>
<!-- pom.xml extract -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cxx-maven-plugin</artifactId>
<version>0.0.5-SNAPSHOT</version>
<configuration>
<sourceDirs>
<sourceDir>${basedir}/src</sourceDir>
</sourceDirs>
<!-- All of the sub configuration nodes following are needed by sonar-cxx plugin -->
<!-- This sample provided values are default values. -->
<!-- So you can simple remove all of it if you provide reports in the right places -->
<xunit>
<directory>${basedir}/reports/xunit-reports</directory>
<includes>
<include>**/xunit-result-*.xml</include>
</includes>
</xunit>
<gcovr>
<directory>${basedir}/reports/gcovr-reports</directory>
<includes>
<include>**/gcovr-reports-*.xml</include>
</includes>
</gcovr>
<cppcheck>
<directory>${basedir}/reports/cppcheck-reports</directory>
<includes>
<include>**/cppcheck-result-*.xml</include>
</includes>
<reportsIncludeSourcePath>
<include>..</include>
</reportsIncludeSourcePath>
</cppcheck>
<cppncss>
<directory>${basedir}/reports/cppncss-reports</directory>
<includes>
<include>**/cppncss-result-*.xml</include>
</includes>
<reportsIncludeSourcePath>
<include>..</include>
</reportsIncludeSourcePath>
</cppncss>
<veraxx>
<directory>${basedir}/reports/vera++-reports</directory>
<includes>
<include>**/vera++-result-*.xml</include>
</includes>
<reportsIncludeSourcePath>
<include>..</include>
</reportsIncludeSourcePath>
</veraxx>
<valgrind>
<directory>${basedir}/reports/valgrind-reports</directory>
<includes>
<include>**/valgrind-result-*.xml</include>
</includes>
<reportsIncludeSourcePath>
<include>..</include>
</reportsIncludeSourcePath>
</valgrind>
</configuration>
</plugin>
</plugins>
<!-- We have our own <configuration><sourceDirs> node inside cxx plugin configuration -->
<!-- <sourceDirectory>${basedir}/sources</sourceDirectory> -->
</build>
<properties>
<sonar.language>c++</sonar.language>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
</properties>
</project>

Resources