Disable escaping replacement values in build-helper:regex-property - regex

I want to replace a property in Maven based on a regex. For that I am using the regex-property plugin. Property will contain space-separated entries and I need to create a xml "node" from each of them.
"C:\some\entry D:\another\entry"
(processing here ... below is the content of variable after processing)
<fileset dir="C:\some\entry" includes="*.myext" />
<fileset dir="D:\another\entry" includes="*.myext" />
The replaced property then should be later used to copy given artifacts:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>copy files</id>
<phase>initialize</phase>
<configuration>
<tasks>
<copy todir="${project.basedir}/somedir">
${processedPaths} <!-- THIS WILL EXPAND TO <fileset ... /> -->
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I have something that almost works:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>testprop</name>
<value>${testprop}</value>
<regex>([^\s]+)</regex>
<replacement><fileset dir="$1" includes="*.myext" /></replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
But the problem here is that the replacement is escaped somewhere along the way. So the resulting property would contain <fileset dir\="C\:\\some\\entry" includes\="*.myext" />, which is not desired.
This approach does seem hackish, but I could not find any other way that would allow me to copy files from directories specified in a property.

I did not mention an important thing - this project is being created from an archetype. Generating a project from an archetype means one can use Velocity syntax. This simplifies my particular usecase quite a bit. The working exerpt of pom.xml looks like this:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>copy files</id>
<phase>initialize</phase>
<configuration>
<tasks>
<copy todir="${project.basedir}/${somedir}">
#foreach( $file in $filesPath.split(",") )
<fileset dir="$file.trim()" includes="*.myext"/>
#end
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The #foreach directive will be picked up by Velocity and will print out a <fileset ... line for each comma-separated entry in the $filesPath property.
And in archetype-metadata.xml is declared:
<requiredProperty key="filesPath"/>
Calling mvn archetype:generate ... "-DfilesPath=/some/path/, /other/path" will then generate the correct nodes:
<copy todir="${project.basedir}/${somedir}">
<fileset dir="/some/path" includes="*.myext"/>
<fileset dir="/other/path" includes="*.myext"/>
</copy>

Related

How to specify the specific packages for the 'jacoco-check'?

My project is built using Maven. I use the 'Jacoco' plugin to perform quality checks.
For a project I would like to check the test coverage on line basis. I would like to check the line coverage only for only 3 packages. How can I specify this check?
I tried 'including' a number of packages, but that does not work.
I also tried to include the root package level and exclude a number of other packages. Also not working.
How can I have the package A, B and C checked? See my example below:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
...
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<includes>
<include>nl.abc.xyz.package-a.**</include>
<include>nl.abc.xyz.package-b.**</include>
<include>nl.abc.xyz.package-c.**</include>
</includes>
...
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.30</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
includes and excludes of rule are about name of the corresponding element.
In case of <element>PACKAGE</element> they are about package name.
And therefore
<includes>
<include>nl.abc.xyz.package-a.**</include>
<include>nl.abc.xyz.package-b.**</include>
<include>nl.abc.xyz.package-c.**</include>
</includes>
Matches for example package named nl.abc.xyz.package-a.something, but doesn't match nl.abc.xyz.package-a.
Given
src/main/java/org/example/a/A.java
package org.example.a;
public class A {
}
src/main/java/org/example/a/B.java
package org.example.b;
public class B {
}
src/test/java/ExampleTest.java
public class ExampleTest {
#org.junit.Test
public void test() {
new org.example.a.A();
}
}
and pom.xml
<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>org.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<includes>
<include>org.example.b</include>
</includes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Execution of mvn verify will fail as expected
[INFO] --- jacoco-maven-plugin:0.8.2:check (check) # example ---
[INFO] Loading execution data file /private/tmp/j/target/jacoco.exec
[INFO] Analyzed bundle 'example' with 2 classes
[WARNING] Rule violated for package org.example.b: lines covered ratio is 0.00, but expected minimum is 0.90
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
and after replacement of <include>org.example.b</include> on <include>org.example.*</include> will also fail with the same message, because org.example.* matches org.example.b. And after replacement on <include>org.example.a</include> will succeed as expected.

Packages from running integration module are not excluded

I´m running some integration test from module A getting first all the classes from module B, what I´m interested is in not add in the coverture some packages from B, but also some package from A.
I´m using excludes in the jacocco plugin, and all the packages that I´m specify from B are exlude perfectly, but the ones from A it dont
Here my config
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>prepare-jacoco-service-test-agent</id>
<!-- default pre-integration is to late for the process-exec-maven-plugin -->
<phase>package</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<propertyName>failsafe.argLine</propertyName>
<includes>
<include>com.in*</include>
</includes>
<excludes>
<exclude>com.B.dg.*</exclude>
<exclude>com.B.es.core.server.vertx.*</exclude>
<!--f2e-core-test-->
<exclude>com.B.es.client.service.*</exclude>
<exclude>com.B.es.core.service.test.*</exclude>
<exclude>com.B.es.core.test.helper.*</exclude>
<exclude>com.B.es.coretest.*</exclude>
<!--f2e-mock-->
<exclude>com.A.f2e.*</exclude>
</excludes>
<classDumpDir>${project.build.outputDirectory}</classDumpDir>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<append>true</append>
</configuration>
</execution>
<execution>
<id>report-jacoco-service-test-results</id>
<goals>
<goal>report-integration</goal>
</goals>
<phase>verify</phase>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.build.directory}/coverage-reports/out/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Any idea why any package of module A it´s not excluded?
By configuring exclusions for prepare-agent-integration you specified that packages should be excluded from instrumentation, but not from report. You need to configure exclusions for report-integration instead.

maven-ear-plugin unpack, filter and repack jarModule

a jarModule that is packaged in my ear file contains a persistence.xml file that I need to remove as part of the package phase. The best I can do is with:
<JarModule>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<unpack>true</unpack> -->
</JarModule>
<packagingExcludes>*/META-INF/persistence.xml,*/META-INF/orm.xml</packagingExcludes>
but I need the jar to be repacked before the packaging the ear.
Any suggestions?
Thanks
You can use the TrueZIP Maven PLugin to remove files from an archive:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>remove-from-jar</id>
<phase>prepare-package</phase>
<goals>
<goal>remove</goal>
</goals>
<configuration>
<fileset>
<directory>${project.build.directory}\<someFolders>\<I-contain-persistence-xml.jar></directory>
<includes>
<include>**\persistence.xml</include>
<include>**\orm.xml</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Archives (*.jar, *.war, ...) can be used like directories with TrueZIP. As shown on the usage page you can move or copy files in archives easily.

Cobertura code coverage is wrong

I am have been trying to setup code coverage on my local Jenkins server with Maven. It would seem that my tests are not being included in the coverage reports.
I have the following in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
<totalLineRate>85</totalLineRate>
</check>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
and here is where I define the tests to be run
<plugin>
<!-- Separates the unit tests from the integration tests. -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
<trimStackTrace>false</trimStackTrace>
</configuration>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*Test*.java</include>
</includes>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
And on my Jenkins server I am have the following:
Jenkins Configuration:
Sonar
Sonar name:sonar local 3.5.1
server url: http://local host:9000
Project level:
publish Cobertura Coverate Report:
xml report pattern: **/target/site.covertura/coverage.xml
But when I run either the maven task "mvn cobertura:cobertura" or view the sonar reports in Jenkins, all the coverage is at 0%, which makes me think my unit and integration tests are not being seen.
Is this a problem with the way my pom separates unit and integration tests?
cobertura can't look into integration test to see code coverage.
Try a look at code coverage by integration tests with sonar
and at separating integration and unit tests
To make a short story : you must launch the jacoco maven plugin who will set two properties, one for unit tests, the another for it tests, and launch surefire and failsafe with these properties.
And after, you can show result in sonar in separated applet.
In my own pom file, you will se a part for enabled jacoco :
<properties>
<!-- Coverage and report -->
<jacoco.destFile.unit>${project.build.directory}/jacoco-unit.target</jacoco.destFile.unit>
<jacoco.destFile.it>${project.build.directory}/jacoco-it.target</jacoco.destFile.it>
<coverage.reports.dir>${project.build.directory}/coverage-reports</coverage.reports.dir>
<version.surefire>2.12.4</version.surefire>
<!-- Sonar -->
<sonar.jacoco.itReportPath>${jacoco.destFile.it}</sonar.jacoco.itReportPath>
<sonar.jacoco.reportPath>${jacoco.destFile.unit}</sonar.jacoco.reportPath>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
</properties>
<build>
....
<plugins>
<!-- Jacoco configuration -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.0.201210061924</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine.unit</propertyName>
<destFile>${jacoco.destFile.unit}</destFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine.it</propertyName>
<destFile>${jacoco.destFile.it}</destFile>
</configuration>
</execution>
</executions>
</plugin>
<!-- And in the surefire and failsafe plugins you need to enable jacoco like this ->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${version.surefire}</version>
</dependency>
</dependencies>
<configuration>
<argLine>${jacoco.argLine.unit}
-Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine>
<forkMode>always</forkMode>
<parallel>classes</parallel>
</configuration>
</plugin-->
<!-- Play the /src/test/java/**IT.java with goal verify -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.surefire}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${version.surefire}</version>
</dependency>
</dependencies>
<configuration>
<forkMode>pertest</forkMode>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<argLine>${jacoco.argLine.it}
-Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
In Jenkins, you must setup the "postbuild" JaCoCo plugins with
"**/**.target" for path to exec files
Hope this will help you.

JWSC for weblogic 12c with Maven

We are in the process of upgrading from Weblogic 10g to 12c. A portion of our code base is webservices so we were using weblogic-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>weblogic-maven-plugin</artifactId>
<version>2.9.5</version>
<configuration>
<contextPath>ws</contextPath>
<keepGenerated>true</keepGenerated>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jwsc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>${weblogic.version}</version>
<scope>system</scope>
<systemPath>${bea.lib}/weblogic.jar</systemPath>
</dependency>
</dependencies>
</plugin>
The build error I see is
[ERROR] Failed to execute goal org.codehaus.mojo:weblogic-maven-plugin:2.9.5:jwsc (default) on project webService: Execution default of goal org.codehaus.mojo:weblogic-maven-plugin:2.9.5:jwsc failed: Plugin org.codehaus.mojo:weblogic-maven-plugin:2.9.5 or one of its dependencies could not be resolved: Failure to find weblogic:webservices:jar:10.3.6 in http://ccicusbuild1/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
Deeper inspection shows that the plugin has a dependency on weblogic:weblogic:10.3.6 and weblogic:webservices:10.3.6. As shown in the previous code, I can override weblogic:weblogic:10.3.6 with weblogic:weblogic:12.1.1. The problem is webservices.jar is no longer apart of weblogic 12c, so I have nothing to override the dependency with, nor can I exclude it.
The page for weblogic-maven-plugin (http://mojo.codehaus.org/weblogic-maven-plugin/) mentions support for 12c, but doesn't give any details.
The goal is to be able to run JWSC through maven. Is there a tweak to the plugin configuration that I can do to make it work, or is there another plugin, or do I need to bite the bullet and run the code with the ant plugin?
This was the eventual solution we used. If some one else has something better, please post it.
plugins portion of pom.xml
<plugins>
<!--
Below contains a work around to build web services for Weblogic 12c.
weblogic-maven-plugin was how things were done (and was much cleaner)
but at the time of this work around, it doesn't appear to support Weblogic 12c.
If in the future, weblogic-maven-plugin or some other plugin become known,
it should replace both parts of the work around.
-->
<!-- START OF WORK AROUND part 1-->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-main-artifact</id>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.artifact.setFile(new File(project.build.directory+'/'+project.artifactId+'-'+project.version+'.war'))
</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<property name="maven.compile.classpath" refid="maven.compile.classpath" />
<property name="maven.runtime.classpath" refid="maven.runtime.classpath" />
<property name="maven.test.classpath" refid="maven.test.classpath" />
<property name="maven.plugin.classpath" refid="maven.plugin.classpath" />
<ant antfile="src/main/ant/build.xml" target="all" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>${weblogic.version}</version>
<scope>system</scope>
<systemPath>${bea.lib}/weblogic.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
<!-- END OF WORK AROUND part 1 -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<!-- START OF WORK AROUND part 2 -->
<executions>
<execution>
<id>default-war</id>
<phase>none</phase>
</execution>
</executions>
<!-- END OF WORK AROUND part 2 -->
</plugin>
</plugins>
build.xml
<project name="build-webservice" default="all">
<target name="all" depends="build.webService" />
<path id="maven_plugin_classpath">
<pathelement path="${maven.plugin.classpath}" />
</path>
<path id="maven_runtime_classpath">
<pathelement path="${maven.compile.classpath}" />
<pathelement path="${maven.runtime.classpath}" />
<pathelement path="${maven.plugin.classpath}" />
<pathelement path="${weblogic.jar}" />
</path>
<taskdef name="jwsc"
classname="weblogic.wsee.tools.anttasks.JwscTask"
classpath="${weblogic.jar}"
classpathref="maven_plugin_classpath"
/>
<target name="build.webService" description="Compile the web services if not up2date">
<!--
Eclipse compiles and places classes into target/classes when the workspace is building.
If this folder exists when jwsc runs, then any classes that are already compiled will NOT
be included in the final WAR file. Thus, this directory is removed prior to created the
webServices WAR fie.
-->
<delete dir="target/classes" />
<jwsc srcdir="${project.build.sourceDirectory}"
destDir="target"
classpathref="maven_runtime_classpath"
keepGenerated="yes"
applicationxml="${project.build.directory}/application.xml"
fork="true"
memorymaximumsize="256m"
verbose="true"
debug="on"
>
<module contextPath="ws" name="${project.artifactId}-${project.version}">
<jwsfileset srcdir=".">
<include name="**/*.java" />
<exclude name="**/*Test.java" />
</jwsfileset>
</module>
</jwsc>
</target>
</project>
Overall description:
War plugin execution is overwritten so that it isn't run
The compiling and packaging is handled in ant by JwscTask
gmaven plugin is used inform maven that it should use the ant generated war as the artifact
Notes:
Alternatives for gmaven were attachartifact ant task and build-helper-maven-plugin as specified in How to register a custom built jar file as maven main artifact?, but neither worked. Both resulted in An attached artifact must have a different ID than its corresponding main artifact.