The maven assembly plugin is not using the finalName for installing with attach=true? - maven-2

I have configured following assembly:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions>
<execution>
<id>${project.name}-test-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.name}-test</finalName>
<filters>
<filter>src/assemble/test/distribution.properties</filter>
</filters>
<descriptors>
<descriptor>src/assemble/distribution.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
<execution>
<id>${project.name}-prod-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.name}-prod</finalName>
<filters>
<filter>src/assemble/prod/distribution.properties</filter>
</filters>
<descriptors>
<descriptor>src/assemble/distribution.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This produced two zip-files:
distribution-prod.zip
distribution-test.zip
My expectation for the property attach=true is, that the two zip-files are installed with the name as given in property finalName.
But the result is, only one file is installed (attached) to the artifact.
The maven protocol is:
distrib-0.1-SNAPSHOT.zip
distrib-0.1-SNAPSHOT.zip
The plugin is using the artifact-id instead of property finalName!
Is this a bug?
The last installation is overwriting the first one.
What can i do to install this two files with different names?
Thanks for your investigation.
Roland

The last installation is overwriting the first one. What can i do to install this two files with different names?
As expected (I don't know if this is a bug or not but that's how the assembly plugin works). To avoid this, you will have to set the appendAssemblyId properties to true and, to obtain an equivalent result, to change the finalName to ${project.name} and the assemby id to test and prod (i.e. to use two assembly descriptors). Something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions>
<execution>
<id>${project.name}-test-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<finalName>${project.name}</finalName>
<filters>
<filter>src/assemble/test/distribution.properties</filter>
</filters>
<descriptors>
<descriptor>src/assemble/distribution-test.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>${project.name}-prod-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<finalName>${project.name}</finalName>
<filters>
<filter>src/assemble/prod/distribution.properties</filter>
</filters>
<descriptors>
<descriptor>src/assemble/distribution-prod.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Where distribution-test.xml and distribution-prod.xml declare assembly id test and prod respectively.

Related

How to resolve Inteliji Maven Configuration error for cucumberOutput?

There is an error in pom.xml with Inteliji, same pom.xml is working fine in Eclipse ,
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>5.6.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>Automation</projectName>
<outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
<cucumberOutput>${project.build.directory}cucumber/cucumber.json</cucumberOutput>
</configuration>
</execution>
</executions>
</plugin>
Error is at <cucumberOutput>${project.build.directory}cucumber/cucumber.json</cucumberOutput>
Error message :
Element cucumberOutput is not allowed here
Version 5.6.0 no longer uses the <cucumberOutput> tag for the configuration.
It has been replaced with:
<inputDirectory>${project.build.directory}</inputDirectory> <!-- this is where the cucumber.json files is saved -->
<jsonFiles>
<param>cucumber.json</param> <!-- the name/regex of the cucumber report json file -->
</jsonFiles>
Here is an example of my configuration:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>5.6.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>App-Under-Test</projectName>
<outputDirectory>${project.build.directory}</outputDirectory>
<inputDirectory>${project.build.directory}</inputDirectory>
<jsonFiles>
<param>cucumber-*.json</param>
</jsonFiles>
<mergeFeaturesById>false</mergeFeaturesById>
<mergeFeaturesWithRetest>false</mergeFeaturesWithRetest>
<checkBuildResult>false</checkBuildResult>
<buildNumber>1</buildNumber>
</configuration>
</execution>
</executions>
</plugin>

Race Condition in JaCoCo merge when generating a report?

So this is something, that a colleague called a "Schroedingers Bug" - it worked fine, until something was pointed out. Something was changed and now the bug is there. And changing back didn't help - the bug is still there :-/
In our Maven project we use JaCoCo for our code coverage (maven-jacoco-plugin version 0.8.7). The surefire plugin (version 2.22.2) is making the unit tests, the failsave (version 3.0.0-M5) is doing the integration tests.
This is our POM:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${maven-jacoco-plugin.version}</version>
<executions>
<execution>
<id>before-unit-test-execution</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</destFile>
<propertyName>surefire.jacoco.args</propertyName>
</configuration>
</execution>
<execution>
<id>after-unit-test-execution</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-unit-test-coverage-report
</outputDirectory>
</configuration>
</execution>
<execution>
<id>before-integration-test-execution</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec</destFile>
<propertyName>failsafe.jacoco.args</propertyName>
</configuration>
</execution>
<execution>
<id>after-integration-test-execution</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec</dataFile>
<outputDirectory>
${project.reporting.outputDirectory}/jacoco-integration-test-coverage-report
</outputDirectory>
</configuration>
</execution>
<execution>
<id>merge-unit-and-integration</id>
<phase>post-integration-test</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}/jacoco-output/</directory>
<includes>
<include>jacoco-integration-tests.exec</include>
<include>jacoco-unit-tests.exec</include>
<!-- <include>*.exec</include>-->
</includes>
</fileSet>
</fileSets>
<destFile>${project.build.directory}/jacoco-output/merged.exec</destFile>
</configuration>
</execution>
<execution>
<id>create-merged-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/merged.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-merged-test-coverage-report
</outputDirectory>
</configuration>
</execution>
<execution>
<id>check</id>
<goals>
<!-- check is bound to the verify phase by default -->
<goal>check</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/merged.exec</dataFile>
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
<exclude>configuration/*</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.min.line.coverage}</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.min.branch.coverage}</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
In between you see the commented part - that is the one that changed.
Now this will sometimes throw an EOFException (only when run locally - not when it is exectued on the Gitlab CI/CD pipeline).
The file in question is the "jacoco-integration-tests.exec". Looking up that file shows, that it has not been finished writing. It is much smaller than the other file in the folder ("jacoco-unit-tests.exec").
As this bug is not reliably produced (just now it worked fine after I deleted the target folder) we suspect some race condition going on. As if JaCoCo is still writing that integration test file and then the next step already wants to access it and that crashes. But I don't really know and information about this is hard to come by. Less so, how to fix it.
Does anyone know where this bug stems from and how to fix it?
Thank you very much.
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte (DataInputStream.java:272)
at org.jacoco.core.internal.data.CompactDataInput.readBooleanArray (CompactDataInput.java:64)
at org.jacoco.core.data.ExecutionDataReader.readExecutionData (ExecutionDataReader.java:150)
at org.jacoco.core.data.ExecutionDataReader.readBlock (ExecutionDataReader.java:116)
at org.jacoco.core.data.ExecutionDataReader.read (ExecutionDataReader.java:93)
at org.jacoco.core.tools.ExecFileLoader.load (ExecFileLoader.java:60)
at org.jacoco.core.tools.ExecFileLoader.load (ExecFileLoader.java:74)
at org.jacoco.maven.ReportSupport.loadExecutionData (ReportSupport.java:83)
at org.jacoco.maven.ReportMojo.loadExecutionData (ReportMojo.java:61)
at org.jacoco.maven.AbstractReportMojo.executeReport (AbstractReportMojo.java:191)
at org.jacoco.maven.AbstractReportMojo.execute (AbstractReportMojo.java:180)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
I hope this is enough to go on. jacoco 0.8.7, Windows 10, JDK 11.0.11. It produces the jacoco.exec file but fails when creating the html report.

How to exclude the feature.source in tycho-source feature plugin configuration using regex?

While using the tycho-source-feature-plugin in pom.xml, the name of the feature with .source is given under excludes. Is it possible to use the regular expression which excludes all the features with .source instead of giving the feature.
<plugin>
<groupId>org.eclipse.tycho.extras</groupId>
<artifactId>tycho-source-feature-plugin</artifactId>
<version>${tycho-source-feature-plugin-Version}</version>
<executions>
<execution>
<id>source-feature</id>
<phase>package</phase>
<goals>
<goal>source-feature</goal>
</goals>
<configuration>
<excludes>
<feature id="myfeature.source" />
</excludes>
<labelSuffix></labelSuffix>
</configuration>
</execution>
</executions>
<configuration>
<labelSuffix></labelSuffix>
</configuration>
</plugin>

Share test resources between maven projects

There is a clear solution for sharing the common test code between maven projects using test-jar goal of maven-jar-plugin plugin (see here).
I need to do the similar thing with test resources, in particular, I want test resources of project A be available in the classpath of project B during testing.
For project A one need to declare:
<!-- Package and attach test resources to the list of artifacts: -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<jar destfile="${project.build.directory}/test-resources.jar">
<fileset dir="${project.basedir}/test-resources" />
</jar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/test-resources.jar</file>
<type>jar</type>
<classifier>test-resources</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
And in project B it will be normal dependency:
<dependency>
<groupId>myproject.groupId</groupId>
<artifactId>myartifact</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>test-resources</classifier>
<scope>test</scope>
</dependency>
Question: Should it work in all cases? Is it possible to pack resources without maven-antrun-plugin (using more 'lightweight' plugin)?
Just use jar:test-jar and declare the resulting JAR as a dependency (refer to this guide for more details). And while I don't understand the problem of having resources and classes in this jar, you can always exclude all .class files:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
And to use it:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
Accepted answer helped me, but it's not quite accurate in case you need regular jar of same project as well. It will delete *.class files from both jars.
Settings below translates to something like:
create me 2 jars: 1 regular, 1 test;
remove *.class files, but only from test jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Using maven-dependency-plugin we can put the resource needed in the right directory, only modifying the pom on dependent project is needed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-test-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>dependeeGroupId</groupId>
<artifactId>dependeeArtifactId</artifactId>
<version>dependeeVersion</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
<includes>resourceNeeded.txt</includes>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
type is used to get test resource
outputDirectory is used to put the resource usable in tests
Documentation here: https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html
There is already a goal to build a test jar from maven.
Assuming you need something a little more flexible, you can use the jar plugin to package your test resources and run that goal with the main package goal with something like this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>test-resources</classifier>
<includes>
<include>**/*.whatever-you-want</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Whatever you want bundled would be added to the project-name-version-test-resources.jar when the jar goal is run.
You could then include it in a project via the same dependency you use above.

Maven WAR plugin not reading configuration when running in <execution> tag

I'm trying to get Maven to perform several executions with the WAR plugin. It works fine as long as it's defined in the following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>1.0</version>
<configuration>
(...)
</configuration>
But not in the following manner
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
(...)
</configuration>
</execution>
</executions>
</plugin>
Where Maven can't find any of the resources I defined in the <configuration> tag. Have I missed anything important, and/or is there a better way of constructing multiple WAR files in a single build?
I didn't see how to turn off the war that's generated by default, but you can use one configuration outside the <executions> element and the rest inside:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<classifier>with-junk</classifier>
<!-- temp directory that the webapp is assembled in (each must be different) -->
<webappDirectory>${project.build.directory}/build-with-junk</webappDirectory>
<webResources>
<resource>
<directory>junk</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<id>add-other-junk</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<!-- exclude prior configuration -->
<inherited>false</inherited>
<configuration>
<classifier>with-other-junk</classifier>
<webappDirectory>${project.build.directory}/build-other-junk</webappDirectory>
<webResources>
<resource>
<directory>other-junk</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</execution>
</executions>
</plugin>
For me, this builds artifact-0.1-with-junk.war and artifact-0.1-with-other-junk.war and both have the correct files included.
The second version applies the configuration only to the phase you've specified. I'm not able to confirm this right now, but I'd guess it is not being applied because you haven't specified a goal for the configuration to be applied to.
If you add the war goal definition into the execution does it get applied? Like so:
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
(...)
</configuration>
</execution>
</executions>