How to combine multiple build plugins into a single import? - maven

For different projects, I am often using the same build plugins such as:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Is it possible to combine these into a single maven plugin so that all I would need is something like:
<build>
<plugins>
<plugin>
<groupId>my.awesome.group</groupId>
<artifactId>my-awesome-plugin</artifactId>
<version>1.0.0.RELEASE</version>
</plugin>
</plugins>
</build>
Thanks for your help!

Related

VScode maven not executing plugins

I am trying to get my library uploaded to the maven central repository using VScode. In order to meet the requierments for that, I have a bunch of plugins configured in my pom.xml file.
Here are all the plugins:
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
However when I go through the entire maven lifecycle using the VScode maven plugin, no source or javadoc jars are created, and nothing is signed using gpg, what is the issue here?
I guess you put the whole section into <pluginManagement>, right?
Then it is just a configuration and will not be run.
Move it out of <pluginManagement> and it will run as expected.

Exclude methods from delombok for Javadoc purposes

I am working on documentation using the Maven Javadoc plugin, including a delombok phase to document getters and setters, then push to gh-pages. However, this pulls in other lombok generated methods such as equals. Is there a way to specify only a subset of methods to be documented after delomboking?
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>3.1.12.2</version>
<dependencies>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.0.0-beta4</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.10.0</version>
<executions>
<execution>
<id>delombok</id>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
<configuration>
<addOutputDirectory>false</addOutputDirectory>
<encoding>UTF-8</encoding>
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<bottom>Copyright © {currentYear}. All rights reserved.</bottom>
<show>public</show>
<sourcepath>module-a/target/delombok;
module-b/target/delombok;</sourcepath>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-publish-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<checkoutDirectory>${project.build.directory}/scmpublish</checkoutDirectory>
<checkinComment>Publishing javadoc for ${project.artifactId}:${project.version}</checkinComment>
<content>${project.javadoc.directory}</content>
<skipDeletedFiles>true</skipDeletedFiles>
<pubScmUrl>scm:git:git#github.com:MyNamespace/MyProject.git</pubScmUrl>
<scmBranch>gh-pages</scmBranch>
</configuration>
</plugin>
</plugins>

Maven-dependency-plugin (unpack-dependencies) ignores configuration

The following problem occurred:
There is a dependency in the project. The dependency contains .js and .css files (essentially they will be used as resources). I need to extract and put these files in a certain place. I thought to use maven-dependency-plugin for this, but it does not use the configuration I specified (use defaults). Please tell me where I could be wrong.
pom.xml:
<dependencies>
<dependency>
<groupId>my.group.Id</groupId>
<artifactId>my-artifact-id</artifactId>
<version>my_version</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<includeGroupIds>my.group.Id</includeGroupIds>
<includeArtifactIds>my-artifact-id</includeArtifactIds>
<includes>**/*.js,**/*.css</includes>
<outputDirectory>${project.basedir}/my/path</outputDirectory>
<overwriteReleases>true</overwriteReleases>
<overwriteSnapshots>true</overwriteSnapshots>
<overwriteIfNewer>true</overwrteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
You are declaring your plugin execution inside a <pluginManagement> section. This section is great for putting configuration in one place and reusing it later, but it won't execute your plugin.
Try this:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId> <!-- your example contained a typo. -->
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<includeGroupIds>commons-lang</includeGroupIds>
<includeArtifactIds>commons-lang</includeArtifactIds>
<includes>**/*.js,**/*.css</includes>
<outputDirectory>${project.basedir}/my/path</outputDirectory>
<overwriteReleases>true</overwriteReleases>
<overwriteSnapshots>true</overwriteSnapshots>
<overwriteIfNewer>true</overwriteIfNewer> <!-- Typo in your POM here as well -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>

Spreading flyway profiles over phases

I'm trying to create separate profiles which using flyway-maven-plugin, but phase definition doesn't work properly. Which mean that when i use both profiles i have an error on execution because i guess "drop-create-database" using configuration from "migrate-database" thus it`s failed. Does anyone have an idea how to fix it?
<profiles>
<profile>
<id>drop-create</id>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
<driver>net.sourceforge.jtds.jdbc.Driver</driver>
<table>MIGRATION_LOG</table>
<sqlMigrationPrefix>EMP_</sqlMigrationPrefix>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>drop-create-database</id>
<!-- Need to garantee order of execution -->
<phase>package</phase>
<goals>
<goal>clean</goal>
<goal>migrate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>migrate</id>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
<driver>net.sourceforge.jtds.jdbc.Driver</driver>
<table>MIGRATION_LOG</table>
<sqlMigrationPrefix>ALL_</sqlMigrationPrefix>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>migrate-database</id>
<phase>pre-integration-test</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You have to specify the configuration per execution instead of per plugin. Otherwise a later configuration for the same plugin will overwrite previous ones.
This means your pom.xml should look something like this:
<profiles>
<profile>
<id>drop-create</id>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>drop-create-database</id>
<!-- Need to garantee order of execution -->
<phase>package</phase>
<goals>
<goal>clean</goal>
<goal>migrate</goal>
</goals>
<configuration>
<driver>net.sourceforge.jtds.jdbc.Driver</driver>
<table>MIGRATION_LOG</table>
<sqlMigrationPrefix>EMP_</sqlMigrationPrefix>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>migrate</id>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>migrate-database</id>
<phase>pre-integration-test</phase>
<goals>
<goal>migrate</goal>
</goals>
<configuration>
<driver>net.sourceforge.jtds.jdbc.Driver</driver>
<table>MIGRATION_LOG</table>
<sqlMigrationPrefix>ALL_</sqlMigrationPrefix>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Maven and emma plugin

How to configure maven build to fail if some of bundles to instrument are misspelled or no target/coverage/coverage.xml file report is generated?
I did not check, try:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-verifier-plugin</artifactId>
<version>1.0</version>
<configuration>
<verificationFile>target/coverage/coverage.xml</verificationFile>
</configuration>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources