How to include workspace artifacts in the assembly using Appassembler maven plug-in? - maven

I'm using Eclipse m2e in my development environment, and I have a spring-boot maven project(can be viewed as a standard maven jar project with runnable main class in this context) which depends on another maven project in the same workspace(workspace artifact, let's call it moduleB, a sibling of the spring-boot project), when I run the maven goal clean package(the appassembler:assemble goal can be ommited because I configured the execution section of the plugin, see the configuration detail below), the generated assembly in the target directory seems fine, except that the jar of moduleB is missing in the repo. It seems that the plugin is trying to copy every file under the class folder in moduleB according to the log:
...
[INFO] Installing artifact ...
[INFO] Installing artifact /foo/bar/moduleB/target/classes to /foo/bar/repo/groupid/artifactid/0.0.1-SNAPSHOT/moduleB-0.0.1-SNAPSHOT.jar
[INFO] Installing ...
...
How to resolve this? Do I have to install moduleB into the maven local repository before running the assemble? Is there any way to bypass this step because I don't want to mess up the repository with unstable artifacts.
P.S. configuration of the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<configurationDirectory>conf</configurationDirectory>
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<assembleDirectory>${project.build.directory}/someApp</assembleDirectory>
<extraJvmArguments>-Xms128m</extraJvmArguments>
<logsDirectory>logs</logsDirectory>
<repositoryLayout>default</repositoryLayout>
<repositoryName>repo</repositoryName>
<showConsoleWindow>true</showConsoleWindow>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<binFileExtensions>
<unix>.sh</unix>
</binFileExtensions>
<programs>
<program>
<mainClass>someClass</mainClass>
<id>app</id>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
</program>
</programs>
</configuration>
<executions>
<execution>
<id>assemble</id>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
Update #1:
I bypass the spring-boot:repackage goal because it encapsulates everything in one jar, including the configuration files which I want to be conveniently editable in production environment. Here's the earlier question I asked: Alternatives to distribute spring-boot application using maven (other than spring-boot:repackage)

Related

Using maven-deploy-plugin to deploy Eclipse product

I am trying to deploy an Eclipse product to Nexus repository. I am somewhat successful, but still get some errors that I am not sure how to handle.
I am using maven-deploy-plugin in the feature (F) that builds product:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>snapshots</repositoryId>
<packaging>zip</packaging>
<generatePom>true</generatePom>
<url>http://repo:8081/nexus/content/repositories/snapshots</url>
<groupId>my.group</groupId>
<artifactId>my.artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<file>
target/products/product.zip
</file>
</configuration>
</execution>
</executions>
</plugin>
I execute 'mvn clean install' to build the product and then 'mvn deploy' on F. If I do that then I get error
Exception while publishing product /home/akravets/dev/workspaces/trunk/my.repository.feature/myProduct.product: publishing result: [Included element my.product.feature.feature.group 8.0.1.R20180301-1431 is missing. Cannot determine filter for requirement to this element.] -> [Help 1]
I've read discussions about this issue, but nothing seems relevant to my issue because I don't have any defined deploy phases besides the one in maven-deploy-plugin.
If I change phase of maven-deploy-plugin to 'install' I get almost satisfactory results. The zip file and generated pom get deployed to repository, but the build fails with error
product.qualifier-p2artifacts.xml. Return code is: 400, ReasonPhrase: Bad Request.
Why does this file even gets considered to be deployed to repository when it's not in file element of maven-deploy-plugin and it's not in the directory structure where zip file is: target/ vs target/product/product.zip? Is this because maven-deploy-plugin is executed during the install phase and it picks up all generated resources as candidates for deployment?
I simply recommend you to use Maven-Tycho to deploy Eclipse RCP product.Tycho determines the dependencies of a plug-in via the MANIFEST.MF file of the plug-in and fetches the required plugins-bundles through the online repositories.
You can check this git repository as an example; rcp - tycho

how to add jar dependency to xtext maven build

What is the correct way to use a maven jar file in my xtext dsl project?
What I have tried is this:
use the maven-dependency-plugin in the pom.xml file of the *.dsl project to download the .jar file from a maven repository into the ./lib/ directory. This is done as early as possible in the build process: in the maven validate phase
in MANIFEST.MF: add the jar to the classpath: e.g. Bundle-ClassPath: ., lib/value-2.5.6-annotations.jar
in build.properties: add it to the bin.includes
The problem is, that the build only works when I call mvn install twice.
The first time, the .jar file is downloaded to the lib directory as expected (early in the build process), but then the build fails because it cannot resolve the types in my jar file.
When I then run mvn install again (the .jar file now already exists in the lib directory before the build), it works fine.
Any ideas how to resolve this?
Short answer
Currently it does not work as expected, because of bugs in Tycho
#353889: Defer target&dependency resolution to the normal build
#393978 maven-dependency-plugin:copy-dependencies goal does not work reliably with Tycho projects - "error copying ....jar.jar"
Long answer
Here is what I did to make it work (for now) in the xxx.dsl project:
pom.xml file
I use the maven-dependency-plugin to download the jar file in the maven validate phase (as early in the build as possible) to the lib directory.
Note, that I use stripVersion=true so that the file in the lib dir is called value-annotations.jar (and not value-2.5.6-annotations.jar). If I ever want to update the version in the future, I only need to update it in one place in the pom.xml file.
The jar file must also be specified as a dependency, because otherwise the users of the dsl plugin cannot build the project: i.e. the generateXtext task of the xtext-gradle-plugin will fail because it cannot find the classes in the jar file.
Relevant pom.xml code:
<project ...>
<properties>
<xtextVersion>2.13.0</xtextVersion>
<immutablesVersion>2.5.6</immutablesVersion>
...
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-libraries</id>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
<outputDirectory>lib</outputDirectory>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
...
</build>
<dependencies>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
</dependency>
</dependencies>
</project>
META-INF/MANIFEST.MF file
Add the jar file to the Bundle-ClassPath, so that we can use it: e.g. in the DslJvmModelInferrer.xtend
Add the package of the jar file to Export-Package, so that these files can be accessed by the xxx.dsl.tests project
Relevant parts of MANIFEST.MF:
Bundle-ClassPath: ., lib/value-annotations.jar
Export-Package: xxx.xtext,
...
xxx.xtext.validation,
org.immutables.value
build.properties file
Add the jar file to the bin.includes so that it will be copied to the generated jar file (in the target directory):
bin.includes=model/generated/,\
.,\
META-INF/,\
lib/value-annotations.jar,\
plugin.xml
Build
Now the build works in Eclipse.
On the command line (and in my continuous integration server script), I must execute maven twice (because of the mentioned bugs):
mvn verify (to download the jars)
mvn install

Exclude non-Java JARs from Maven Javadoc plugin processing

I have a multi-module project which builds a couple of JARs and then distributed them into two WARs.
However, one of the JARs does not contain any Java code at all, merely (maven-filtered) resources. This leads to warnings during the build like these:
[…]
[INFO] --- maven-javadoc-plugin:2.8:jar (attach-javadocs) # foo-services ---
[INFO] The goal 'org.apache.maven.plugins:maven-javadoc-plugin:2.8:javadoc' has not been previously called for the module: 'de.tarent.foo:foo-rsrcs:jar:1.3.900-SNAPSHOT'. Trying to invoke it...
[WARNING] Creating fake javadoc directory to prevent repeated invocations: /var/lib/jenkins/jobs/FooTool/workspace/foo-backend/foo-rsrcs/target/apidocs
[ERROR] Error fetching link: /var/lib/jenkins/jobs/FooTool/workspace/foo-backend/foo-rsrcs/target/apidocs/package-list. Ignored it.
[INFO]
Loading source files for package de.tarent.foo.rest.transformation...
[…]
I found how I can exclude javadoc stuff by package, but not by artifact.
The plugin is currently included ONLY in the parent POM, like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
While it is run during “compilation” of the resources-only JAR, it (obviously) produces no result, thus the warning (as it’s not excluded either).
How can I either make it produce something or, probably preferably, exclude the foo-rsrcs module?
Giving such a resources dependency the scope runtime should be sufficient to fix this issue. It will actually have better effects for other plugins too.

Always run proguard-maven-plugin before install phase

What I am trying to do, is to obfuscate a certain packages in a multi module application, before it gets installed to my local repository, so that the final package will be an EAR file which contains obfuscated jars.
I tried to obfuscate the jars during EAR building process without success. Now i want to build the EAR with obfuscated jars instead ob obfuscating then during the build.
So I've got the following plugin configuration:
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>${version.proguard}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
...
</configuration>
</plugin>
So there are two problems for me:
Progruard always runs after the install phase, so that the EAR build always gets the not obfuscated jars
I always have to add proguard:proguard to the maven command, which of course fails in a multi module project where some modules don't have to be obfuscated
So my questions:
How can I obfuscate the package before it gets installed?
How can I make plugins like this one run on default without adding <phase>:<goal> to the maven call?
Thnx.
It seems that for the proguard plugin to work, JAR files are needed. Perhaps you can achieve this by attaching the proguard plugin's proguard goal to the package phase (and not process-classes phase) of the default Maven build life cycle as proposed here by Alexey Shmalko. It's not clear to me if you are using the maven-shade-plugin, but if you are, then place the proguard plugin configuration your in pom.xml after that of maven-shade-plugin (this is because both these plugin attach to the same phase: package).
My expectation is that since package phase is achieved before install phase, it should give you the effect you are looking for.

Sonar configuration in multimodule maven project using tycho for unit tests and jacoco for coverage

We're using maven to run a sonar analysis and it works well except for the code coverage results with jacoco. We have an eclipse project that uses tycho-surefire-plugin for testing. I've not overriden the argLines properties so solutions involving that line may not be appropiate.
Facts :
Maven structure structure:
parent
master
module 1
module ...
module n
Testing structure:
client.admin (eclipse-plugin packaging)
client.admin.test.fragment (eclipse-test-plugin packaging)
Properties that are correctly set and identified
sonar.junit.reportsPath
sonar.jacoco.reportPath,
sonar.jacoco.itReportPath
sonar.core.codeCoveragePlugin
sonar.language
The main problem is with the following properties
sonar.test
sonar.sources
sonar.java.binaries
As seen in the Testing structure in the client.admin.test.fragment tests are contained in the /src folder and the sources are located in the project client.admin in the /src folder too.
When we run the analysis we get the following error :
[WARN] Coverage information was not collected. Perhaps you forget to include
debug information into compiled classes?
I believe this has to do with the properties sonar.java.binaries that goes looking for the sources in target/classes of the fragment project (client.admin.project) that are in fact located in the host project (client.admin). In the fragment project we've configured sonar.tests and sonar.sources properties so that they call the /src folder of the corresponding projects.
In the sonar Analysis Parameters page there says that only sonar.sources is a maven valid property, sonar.tests and sonar.java.binaries cannot apparently be configured in maven. How then could I attach the binaries to the project. I've tried copying the folder target/classes from the host project but I got the same message. Is there any workaround in maven ?
Edit 1
There is one jacoco.exec file that is generated for the whole project that can be found at the parent folder. This was done configuring the jacoco.destFile and sonar.jacoco.reportPath properties
Jacoco plugin in main pom :
<!-- Jacoco Plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<outputDirectory>${jacoco.reports.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Maven Plugin Versions:
sonar: 2.4
jacoco: 0.7.1.201405082137
Properties
<sonar.language>java</sonar.language>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.junit.reportsPath>${project.build.directory}/surefire-reports/</sonar.junit.reportsPath>
<sonar.jacoco.reportPath>${basedir}/../../../main/**.master/target/jacoco.exec</sonar.jacoco.reportPath>
<jacoco.reports.outputDirectory>${basedir}/../../../main/**.master/target/site/jacoco</jacoco.reports.outputDirectory>
<sonar.sources>src</sonar.sources>
In the test projects (eclipse-test-plugin) we changed added the property sonar.sources to go find the sources from the src folder of the project that we're testing for example in client.admin.test.fragment we go search the src from the client.admin
The following properties were commented in code because they're not supported in maven according to documentation and to the debug output.
<!--<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>-->
<!--<sonar.tests></sonar.tests>-->
<!--<sonar.java.binaries></sonar.java.binaries>-->
First, you must tell the JaCoCo agent to report all coverage data into one common file. Second, you tell the Sonar JaCoCo plugin to read the coverage data from the aggregated file.
To do so, set the properties "jacoco.destFile" and "sonar.jacoco.reportPath" in your parent pom.xml to the same absolute path, e.g.:
<properties>
<jacoco.destFile>/home/jenkins/jobs/my.project/workspace/parent/target/jacoco.exec</jacoco.destFile>
<sonar.jacoco.reportPath>/home/jenkins/jobs/my.project/workspace/parent/target/jacoco.exec</sonar.jacoco.reportPath>
</properties>
Note that these properties will be inherited to all child poms, so you can't use Maven expressions like ${project.build.directory} because this would evaluate to a different directory for each pom.
You could create a small helper Mojo which automatically resolves an absolute path on the current build machine and then injects the properties into the Maven model.

Resources