buid-helper-plugin with multiple active profiles - java

I have a project which may take different sources / resources based on the selected profiles. Some profiles are mutually exclusive, some should be combinable, for example the profiles local and wildfly defined in the snippet below should be combinable
<profiles>
<profile>
<id>local</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>profiles/local/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>profiles/local/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>wildfly</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>profiles/wildfly/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>profiles/wildfly/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
If I do this under eclipse I notice that only the latter is actually applied - I only see the sources / resources defined in the wildfly profile.
Also in the resulting complete POM I notice that only the latter (wildfly) profile's configuration is applied, thus removing the sources and the resource defined in the local profile.
Is this the way the plugin is supposed to work, or am I missing something?

Ok.... It was just me.
I gave the executions the same name, hence they were overridden. My bad.
This question might be closed / removed by moderators.

Related

Run maven building profile without building, just run exec-maven-plugin

I created 4 run profiles that contain only the maven-exec plugin with goal java to run my war with different startup parameters. Problem is that profiles are always triggering rebuilding my war file, and it causes failure because the war file is in use when at least one of these profiles is already running. I don't want to rebuild war when I run these profiles. Is it possible to run the build profile without building war and just run the plugins in the profile?
<profiles>
<profile>
<id>devAdmin</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>main.Main</mainClass>
<arguments>
<argument>-admin</argument>
</arguments>
...
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>devCluster</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>main.Main</mainClass>
<arguments>
<argument>-cluster</argument>
</arguments>
...
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>devClient</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>main.Main</mainClass>
<arguments>
<argument>-client</argument>
</arguments>
...
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>devWeb</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>main.Main</mainClass>
<arguments>
<argument>-web</argument>
</arguments>
...
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Thank you so much for help :)
Execute just the single goal: mvn exec:java -PdevClient

How to provide a list of artifacts to the maven-dependency-plugin:copy goal through a variable?

I'm trying to do this:
<project>
[...]
<properties>
<my-artifacts>org.junit:junit,org.slf4j:slf4j</my-artifacts>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
${my-artifacts}
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Is it possible somehow?
First you have to configure the artifactItems because artifactItems means there are several of them. To define a single artifact you have to define artifactItem as part of the list...like the following:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
You can not define a list of artifact via a property. You have to define them in the configuration. You could define a single artifact via a property if you change your configuration according to the above.
https://maven.apache.org/plugins/maven-dependency-plugin/usage.html#dependency:copy

Replace a constant in JAVA code with Maven without modifying the original code

I have a text in a java constant that I want to be replaced according to a maven variable that is configured when generating the artifacts in the following way:
public class FOO {
public static final String BASE = "/#FOO#";
}
The problem is that if I replace the java code, it is replaced forever and the replacement is no longer performed, so if I change the value of the variable it has no effect:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
I have fixed this by doing the process in reverse:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>first-execution</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</execution>
<execution>
<id>second-execution</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>${my.custom.property}</token>
<value>#FOO#</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>
But this second step can be dangerous, as there can be conflicts and replace something that has the same value in the java code of the class.
Another alternative would be to replace in the .class files as follows:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/target/my-artifact-directory/WEB-INF/classes/com/my/package//Constants$PATHS.class</include>
</includes>
<replacements>
<replacement>
<token>#FOO#</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
The replacement works but the application does not start correctly.
Any other ideas on how to perform the replacement without modifying the original code?
I'd create a resource file in src/main/resources that is read in FOO and use the Maven Resources Plugin's Resource Filtering similar to the answer to spring maven profile - set properties file based on compilation profile:
foo.properties
BASE=${propertyName}
pom.xml
<project>
...
<properties>
<propertyName>property value</propertyName>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
...

Corrupt JARS in zip using maven

I have maven project that i want to package in a zip file including the jar binaries(in a folder /jars). After the packaging, I've unzipped and extracted the jar file but get the below error when i invoke this:
PS C:\Users\kenochrome\Downloads> jar xf service-0.1.jar
java.util.zip.ZipException: invalid literal/length code
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:139)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:142)
at sun.tools.jar.Main.extractFile(Main.java:715)
at sun.tools.jar.Main.extract(Main.java:678)
at sun.tools.jar.Main.run(Main.java:191)
at sun.tools.jar.Main.main(Main.java:904)
Below is a snippet of parent and child poms.
Parent POM
<build>
<finalName>release_1.0</finalName>
<resources>
<resource>
<directory>${basedir}/hello/world/00006/jars</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>assembly/bin.xml</descriptor>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Child POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>scala-compile</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<finalName>service-${project.version}</finalName>
<outputDirectory>../../jars</outputDirectory>
<resources>
<resource>
<directory>jars</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
I'm sorry if the information is not adequate or if this question is not correctly formatted.
Many thanks for going through this post and much appreciated in advance.
Was looking through another forum and found out that the issue that I was having was using the <lineEnding>unix</lineEnding> element in my descriptor assembly xml.

How to convert snippets to asciidoc html in eclipse

how to convert generated snippets to ASCII-doc html in spring boot.
i have already tried added the ASCII plugin.
i have already created .adoc file in src/main/asciidoc, but after maven build, it does not generate the html
include::{snippets}/test_find_by_id/http-request.adoc[]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Documentation.java</include>
</includes>
<systemPropertyVariables>
<org.springframework.restdocs.outputDir>
${snippetsDirectory}
</org.springframework.restdocs.outputDir>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
<attributes>
<snippets>${snippetsDirectory}</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<!-- <phase>process-resources</phase>-->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.outputDirectory}/static/docs
</outputDirectory>
<resources>
<resource>
<directory>
${project.build.directory}/generated-docs
</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
You need to reference the ASCII Doc Maven plugin in your pom.xml. See: https://github.com/asciidoctor/asciidoctor-maven-plugin#installation
mvn prepare-package
is the solution

Categories

Resources