Regarding updating pom file - maven

I am working on a continuous integration tool and here i need to integrate jacoco plugin after checking out. I do not want to enforce developer to add jacoco plugin while checkin instead i would like to add it while running the jenkins based continuous integration tool.
Is it possible to achieve any of these options?:
1. to modify the pom file checked in by the developer and inject the dependency and plugin required for jacoco?
OR
to add the jacoco plugin super pom and mention that in jenkins configuration.
I tried this super pom approach, and it was reading super pom from a common repository, but not generating jacoco-exec file. I used below in super pom.
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
pom
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destFile>${basedir}/target/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any help will be appreciated. Thanks

You can create a separate profile in the pom.xml, name it as jenkins profile and include jacoco plugin in that profile. You can then configure the mvn command in your jenkins job to include -P jenkins_profile_name. So the build would be done using the jenkins profile configuration.
Mark the existing profile as default and developers won't need to do anything specific.
For more details, refer to maven documentation for build profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Related

custom runtime issue with native-maven-plugin having metadataRepository enabled

I have an app built with Spring Boot 3.0.0-M5, Spring Cloud 2022.0.0-M4, Java 17. Its GitHub repo is https://github.com/wenqiglantz/springboot3-webclient/tree/metadata. I am trying to achieve the following:
Enable metadataRepository configuration in native-maven-plugin in my app to assist in my native image build as this plugin automatically searches for GraalVM reachability metadata repo.
Need to create a custom runtime (in a zip format) using maven-assembly-plugin as I need to deploy my app to AWS Lambda since Lambda doesn't yet support Java 17 runtime.
I was able to generate the custom runtime in a zip file with maven-assembly-plugin and a few config files in my source code and deploy to Lambda (my github actions workflow run https://github.com/wenqiglantz/springboot3-webclient/actions/runs/3146602388/jobs/5115233455). However, after adding native-maven-plugin to enable metadataRepository, my custom runtime build broke, it no longer generates the zip file (github actions workflow run https://github.com/wenqiglantz/springboot3-webclient/actions/runs/3151692666/jobs/5125968700). Also see attached the comparison screenshot. The maven command I use in my workflow is mvn -ntp clean package -Pnative -e -X. In my root pom. Unfortunately I have not been able to find any good documentation on either GraalVM side or Spring Boot side on how to configure such plugins to achieve what I want. I have the plugins configured as the following, let me know what's the best way to handle configuring both maven-assembly-plugin and native-maven-plugin so I can accomplish my two goals listed above.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>native-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/assembly/native.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.14</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
</configuration>
</plugin>
</plugins>
</build>

is it possible to version and deploy a configuration file to nexus via maven commands

I am working on a java project and I would like to version and store a configuration file on nexus. Lets assume the file structure of java project is as below.
src/
conf/application.config
pom.xml
Is it possible to deploy application.config file to nexus when I run mvn clean install. After each build I expect an application artifact and a configuration artifact to be deployed to nexus. Is there any maven plugin for this purpose.
I manged to deploy file with maven-deploy-plugin.
http://maven.apache.org/plugins/maven-deploy-plugin/usage.html
You can find an example below.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy-file</id>
<!-- change to deploy-->
<phase>install</phase>
<goals>
<goal>deploy-file</goal>
</goals>
</execution>
</executions>
<configuration>
<file>app.properties</file>
<repositoryId>stack.example.release</repositoryId>
<url>http://nexusserver/nexus/content/repositories/releases/</url>
<groupId>com.stack.example.config</groupId>
<artifactId>app</artifactId>
<packaging>properties</packaging>
<version>1.0.0</version>
</configuration>
</plugin>
</plugins>
</build>

Is it possible to get the list of active profiles in Maven?

In Maven, is there a way to get a list of the active profiles, say, as a property or as a text file?
More specifically, when I run:
mvn resources:resources -P MyProfile
I want to get the string MyProfile somewhere I can read it into my Java program.
Maven 3.2.1
Thanks
Edit
I attempted to configure the Maven Help plugin to run the active-profiles goal whenever the goal resources:resources is run by configuring an execution to participate in the process-resources phase as shown below. That did not work either ...:
<packaging>jar</packaging>
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You could try this maven plugin. The configuration below will create a text file that will contain the profiles that were active during the build.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
</plugin>

Jacoco Maven Plugin - Plugin execution not covered by lifecycle configuration

I'm new to Maven and want to use the Jacoco Maven Plugin to build my projects.
I've set up an example project with TestNG the only dependency.
Here is part of the pom.xml:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And i get this error:
Plugin execution not covered by lifecycle configuration: org.jacoco:jacoco-maven-
plugin:0.6.2.201302030002:prepare-agent (execution: default, phase: initialize)
What am I doing wrong ?
Cheers
You can ignore the plugin goal, adding something like this to your pom.xml
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only.
It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)
</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<!-- m2e doesn't know what to do with jacoco,
let's ignore it or annoying error markers appear
see http://wiki.eclipse.org/M2E_plugin_execution_not_covered
-->
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
As this is related to the Eclipse Maven plugin, alternatively this can be set locally in Eclipse's preferences. Moving the configuration out of the project's pom file helps the code simple and clean, free of IDE particulars.
Go to Eclipse --> Preferences --> Maven --> Lifecycle Mappings. Add lifecycle-mapping-metadata.xml as the following:
<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
Reload the life-cycle mappings file and then Maven --> Update Project
Eclipse now offers a quick fix to disable the warning and save those in user preferences (in Eclipse --> Preferences --> Maven --> Lifecycle Mappings lifecycle-mapping-metadata.xml as noted by #iker-aguayo ) so you don't have to manually create or edit the file. This is useful in cases where you can't update the pom (such as using an open source project where you cannot commit.)
I eventually chose to ignore the plugin and use the CLI mvn command instead for the test with code coverage.
Inside your Eclipse IDE, right click on the red color-marked warning for the
jacoco-maven-plugin. You should three options in the popup to fix the warning, choose to ignore the warning, that would result an automatically generated section in your pom.xml, that started with a line of comments,
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
At a command line, run the mvn command before each checkin, and that should trigger the test goal with coverage,
$mvn clean package
This problem is specific to Eclipse, as outlined on the M2E wiki. Sorry I can't help more than that, as I don't use Eclipse.

maven-site-plugin not generating apidocs folder with javadocs

I inherited a project that is supposed to build javadoc files and place them in the site directory. This is not being done. I have looked at all the examples I can find and I can't figure out where the configuration is broken.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</reportPlugins>
</configuration>
</execution>
</executions>
</plugin>
Any and all help is greatly appreciated.
You have bound maven site plugin's site goal to prepare-package phase. You have configured javadoc generation in this plugin configuration.
As such, if you run maven's default lifecycle goals like mvn package or mvn install you should get site report with javadocs.
If you ran mvn site, it would skip prepare-package phase to which your plugin configuration is bound and hence would not generate javadoc.

Resources