Maven to include JAR that is generated by that build - maven

Normally we use dependency tag in pom.xml to have Maven include a JAR on the classpath and it also packages that dependency.
What if the JAR is generated in a step of the same project's build? I mean, it's not the compile plugin that generates it, it is a JAR without any java source and an external executable creates it.
I can use maven exec plugin to have my JAR generated and maven install plugin to have it installed to my local repository. But still I can't have it as a dependency in the same project: No matter which phase I put my JAR generator command in, the dependency check will happen before that and fail because the JAR does not yet exist.
Is system scope dependency my best choice? Then I need to give up packaging. And it's deprecated. And the JAR needs to be outside the project directory.
Or the JAR generator must be in a separate pom? Also not very nice because the JAR is only used by this one project.
Can I configure the dependency plugin to defer the dependency check and download to compile phase?
Any other solution?
This pom almost works, but first time I need to install the generated thing manually to the repo.
<?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">
<parent>
<artifactId>something-parent</artifactId>
<groupId>something</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>something-sample</artifactId>
<properties>
<jnbridge.path>C:/Program Files (x86)/JNBridge/JNBridgePro v9.0</jnbridge.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-proxies-jar</id>
<phase>generate-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>jnbproxy.bat</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/proxies.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!--
Yes, these are true system-scope dependencies. JNBridge is expected to be
installed on the system wherever this project is built.
-->
<dependency>
<groupId>com.jnbridge.org.apache</groupId>
<artifactId>bcel</artifactId>
<version>5.1</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/bcel-5.1-jnbridge.jar</systemPath>
</dependency>
<dependency>
<groupId>com.jnbridge</groupId>
<artifactId>jnbcore</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/jnbcore.jar</systemPath>
</dependency>
<!--
This one will be installed to local maven repo in the process-test-resources phase,
as defined in the project/build/plugins section of this file.
-->
<dependency>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
</dependency>
<!-- other dependencies... -->
</dependencies>
</project>

Related

Pitest report-aggregate goal complains about transitive dependencies

I integrated Pitest according to https://pitest.org/aggregating_tests_across_modules/ in a Maven module project (Maven version 3.5.4).
When running mvn clean package the log shows the Pitest execution for the target modules (i.e. the modules for which Pitest should be executed) and I confirmed that result files are actually produced.
However, I get the following error message for the report module:
[ERROR] Failed to execute goal org.pitest:pitest-maven:1.5.1:report-aggregate (put-it-together) on project mutationtesting:
An error has occurred in PIT Test Report report generation.:
.../.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.0/jackson-databind-2.10.0.jar is not a directory
jackson-databind is a dependency of the target modules and therefore a transitive dependency of the reporting module. It seems to me that the report-aggregate goal also tries to analyse the transitive dependencies of the modules for which Pitest is enabled.
Is there some error in the project configuration? Is this a known bug? What can I do?
I tried to change the scope of the target modules in the reporting module to provided, and I also set the packing of the reporting module to pom, but to no avail.
Configuration
I configured Pitest in the parent POM as follows:
<properties>
...
<pitest.version>1.5.1</pitest.version>
<pitest.junit5.version>0.12</pitest.junit5.version>
<properties>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
<configuration>
<exportLineCoverage>true</exportLineCoverage>
<outputFormats>
<value>XML</value>
</outputFormats>
<timestampedReports>false</timestampedReports>
</configuration>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>${pitest.junit5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
The target module POM's each contain this section:
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
</plugin>
</plugins>
</build>
And finally the reporting module POM looks like this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
...
</parent>
<artifactId>mutationtesting</artifactId>
<dependencies>
<dependency>
target module 1
</dependency>
<dependency>
target module 2
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<executions>
<execution>
<id>put-it-together</id>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

cobertura-maven-plugin cannot find my groovy source code

I am trying to use apache-aven to produce a code-coverage report for my Java/Groovy project. Attached is the pom file:
<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>com.hal_con</groupId>
<artifactId>scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<configuration>
<providerSelection>1.8</providerSelection>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/cobertura-maven-plugin -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</reporting>
</project>
I've tried both the following:
Adding the maven-source-plugin as suggested in: Maven + Cobertura : Unable to locate [Your Class]. Have you specified the source directory?
Adding the jxr-maven-plugin as suggested in:
maven-cobertura-plugion does not show the sources
In both cases the results were exactly the same:
Unable to locate com/hal_con/scheduler/FileParser.groovy. Have you specified the source directory?
I figure that the maven-cobertura-plugin needs to be told where to find my groovy sources, but I cannot find an example.
The Cobertura Maven Plugin doesn't provide a way to customize the location of the sources. By default, it then looks into the Maven standard folder, which is src/main/java. Since your Groovy classes are located inside src/main/groovy, they are not found.
There are 2 solutions depending on your project:
Add those sources to the project with the help of the build-helper-maven-plugin:add-source Mojo:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
This is helpful if the project is a mixed Java / Groovy project, because you can keep the Maven defaults, and add the Groovy specific folders.
Override the source directory of Maven with
<build>
<sourceDirectory>src/main/groovy</sourceDirectory>
<!-- rest of build configuration -->
</build>
This would be convenient if the project is a pure Groovy project, without any source Java files.
With any of those two changes, running mvn clean site will generate a Cobertura report where the Groovy sources will be correctly found.

How to use maven shade to build jar and war

I'm on a project working with openrdf, and I require the shade plugin to transform my service entries. I would like to build a war and a jar simultaneously, as both usages are possible. However, I cannot get the shade plugin to produce a shaded jar and a shaded war at the same time - shade only invokes on the package type defined in the properties, and binding e.g. the jar plugin to the package phase in order to create a jar next to the war results in an unshaded jar. How can I create both a shaded jar and a shaded war at the same time?
If by "shaded war" you mean just the regular war with all dependencies packed into WEB-INF/lib, then you might just use maven-war-plugin separately and use jar as packaging type. This way shade plugin will work correctly. And .war will be built by plugin.
Below is pom.xml. And here is working example.
<?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>so.test</groupId>
<artifactId>stackoverflow-test2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}-fatjar</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.14.9</version>
</dependency>
</dependencies>
</project>

Install tar.gz file in maven repository

I have a compressed tar.gz file and I want use it as dependency for other projects.
I am unable upload it in maven repository with the following command because maven doesn't support tar.gz packaging :
mvn install:install-file -Dfile=/path-to-file/XXX-0.0.1-SNAPSHOT.tar.gz -DpomFile=/path-to-pom/pom.xml
Sample pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<name>XXX</name>
<groupId>com.example.file</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>tar.gz</packaging>
</project>
If I use the above mentioned command with rar packaging then maven does upload XXX-0.0.1-SNAPSHOT.tar.gz file but with .rar extension.
Beside developing maven plugin for custom package, is there any way to upload tar.gz in maven repository and later use it in other project as dependency?
(Note: I just want to use tar.gz and not any other compression e.g rar, etc).
#khmarbaise, thanks you guided correctly. The problem is solved using attached artifact. Here is a snippet from my pom.xml :
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<extensions>true</extensions>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>xxx-0.0.1-SNAPSHOT.tar.gz</file>
<type>tar.gz</type>
<classifier>optional</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Similarly the dependency can be added in other project as follows:
<dependencies>
<dependency>
<groupId>your.group.id</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>optional</classifier>
<type>tar.gz</type>
</dependency>
</dependencies>

Use jarsign plugin with assembly plugin in Maven 3

I'm using assembly plugin to package a list of applets into zip in one of modules with my maven project. here is the pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>applets-deploy</artifactId>
<name>deploy</name>
<dependencies>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>applets-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>ceplot-applet</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
......
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/resources.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
what I need is to also sign jars before they are packaged, can I use jarsign plugin here, and how? I can't find where the jar files are temporarily stored during the build process.
Thanks,
This would be a pretty normal use of the jarsigner plugin. By default, jars are built by the jar plugin during the package phase and output to the ${project.build.directory}, which defaults to target.
You'd just need to sign the jars some time after they're built during package and before you assemble the zip. You could do that by binding the assembly plugin to a later phase or by adding the jarsigner plugin above the assembly plugin and binding it to the package phase, too.

Resources