Is it possible to execute two different maven exec-maven-plugin in a single POM - maven

I execute the following code using mvn exec:java com.mycompany.FooServer.
I would like to add another server which I can execute like mvn exec:java com.mycompany.BarServer.
How do I do that within a single pom file?
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mycompany.FooServer</mainClass>
</configuration>
</plugin>
</build>

Try this. You can have more than one execution under executions. All you need to do is move the configuration element under the execution. The plugin has configuration, but each execution can also have a separate configuration element.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>first-execution</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.FooServer</mainClass>
</configuration>
</execution>
<execution>
<id>second-execution</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.BarServer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
With Maven 3.3.1 and up, you can run an execution by its ID using
mvn exec:java#id
In this case the commands would be mvn exec:java#first-execution and mvn exec:java#second-execution. See this answer for more details.

#tieTYT: You can select the execution by id using two distinct profiles:
mvn test -Pmanager
mvn test -Pproxy
<profiles>
<profile>
<id>proxy</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pt.inesc.proxy.Proxy</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>manager</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pt.inesc.manager.Manager</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

With maven > 3.3.1 it is possible to specify the execution id as:
mvn exec:java#execId

For me including configuration in the execution block didn't work and maven complained about main class not being set. But inspired by Dario's answer I'd answer this question as follows:
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>com.mycompany.FooServer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>com.mycompany.BarServer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Which then allows you to run one or the other server using:
mvn exec:java -Pfoo
or
mvn exec:java -Pbar
Cheers,

I find the solution: I put <configuration> in <execution>
you can use mvn clean test -Pfoo,bar
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>CountContinusIntegr-execution</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.FooServer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>CountContinusIntegr-execution</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.BarServer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

I'm afraid that what you want is not possible. I could not find a way to call the same exec-maven-plugin goal directly (mvn exec:java) with different configurations in .pom file.
Said that, you can however have multiple executions of exec-maven-plugin. The thing is you can not call the goals directly. You have to use multiple executions and bind them to particular build phases.
You could also make use of the following solution that fitted me. You can still call one goal directly with it's configuration in the .pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>Acceptance Tests</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>pybot</executable>
<arguments>
<!--...-->
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<mainClass>pt.jandias.someapp.persistence.SchemaGenerator</mainClass>
<arguments>
<!--...-->
</arguments>
</configuration>
</plugin>
One could than use mvn exec:java and mvn integration-test at will.

Related

Travis CI - Maven build - Tests are skipped by default

I observed that unit tests are skipped by default during travis-ci builds.
My travis config file
language: java
sudo: false
jdk:
- openjdk11
cache:
directories:
- "$HOME/.m2/repository"
- "$HOME/.sonar/cache"
addons:
sonarcloud:
organization: st-spring-samples
token:
secure: ${SONAR_TOKEN}
script:
- mvn org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar
My travis build output:
Can someone please let me know why travis-ci forces -DskipTests=true option in this case?
Was able to overcome the problem by making use of maven build profiles. Assembly plugin will be invoked only with an explicit build profile. So travis install dependencies phase doesn't kickoff assembly process, hence no assembly error.
<profiles>
<profile>
<id>assemble</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.15</version>
<executions>
<execution>
<phase>package</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>${project.artifactId}-mocks-${project.version}</finalName>
<attach>false</attach>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/wiremock-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</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>attach-mock-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/${project.artifactId}-mocks-${project.version}.jar</file>
<type>jar</type>
<classifier>mocks</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.15</version>
<executions>
<execution>
<phase>package</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Full pom.xml can be found here
I am still interested to see if there is a better option available. If anyone knows one, please let me know.

Maven wildfly deployment with multiple servers (standalone)

in my environment I have two wildfly server where I want to deploy with the wildfly-maven-plugin.
The servers differ in the name dev01 and dev02 but have the same port 9993 and username and password.
My understanding is that the wildfly-maven-plugin support only single server deployment.
If the problem are not big enough we use a module/submodule structure where the war file will be build in a submodule.
I'm using two profiles wildfly-deploy-dev01 and wildfly-deploy-dev02.
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev02</id>
<build>
[...]
<profiles>
In the main module I skipped it.
In the war submodule:
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<finalName>${project.artifactId}-v1.0</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<hostname>dev01.example.com</hostname>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
[same as above for hostname dev02.example.com]
</profiles>
First I was thinking everthing works fine but then I found out that only the last server will be deployed.
mvn wildfly:deploy -P wildfly-deploy-dev01,wildfly-deploy-dev02
I played around by setting the configration after the execution tag without success. It looks that the second profile overwrite the first one.
Futher I hardcoded the finalname because the parsedVersion is not parsed.
<finalName>${project.artifactId}-v${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</finalName>
At the moment I'm lost with Maven. Has anybody an idea how I can deploy with the plugin on two servers?
Thanks,
Markus
Ways which I tried:
https://github.com/tsotzolas/wildfly_maven_plugins_examples/blob/master/deployToMultiplesServer/pom.xml
wildfly-maven-plugin not deploying when multiple profiles selected
Cannot access parsedVersion value in pom properties
You should be able to do this in a single profile with different executions. There shouldn't be a need to multiple profiles.
<profiles>
<profile>
<id>wildfly-deploy</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<id>deploy-dev1</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev01.example.com</hostname>
</configuration>
</execution>
<execution>
<id>deploy-dev2</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev02.example.com</hostname>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profiles>
With this you'd just have to do mvn clean install -Pwildfly-deploy.

maven not reading defined properties

Don't know why it is not working. I have a couple of defined properties on the POM and I'm running a little script from it which should be able to read these properties, but it's not happening.
This is the POM:
<properties>
<propery.one>SOMETHING</propery.one>
<propery.two>SOMETHING</propery.two>
<commandline.location>/SOME/PATH/</commandline.location>
<commandline.executable>commandline-script.sh</commandline.executable>
......
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>export</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${commandline.executable}</executable>
<workingDirectory>${commandline.location}</workingDirectory>
<arguments>
<argument>${project.basedir}/scripts/myScript.sh</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'm calling Maven with this command: mvn deploy (which I suppose. it's the correct one)
So, once it starts, I can see a lot of exceptions because the script called on this line: "${commandline.executable}" is not reading the properties from the POM.
Help please...

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>

Generating java from rpc wsdl

I have a pom which generates some java code from an RPC wsdl. The problem is that the code is never generated.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<sourceDirectory>src/main/resources</sourceDirectory>
<outputDirectory>${project.build.directory}/generated/rpc</outputDirectory>
<packageSpace>com.company.wsdl</packageSpace>
<testCases>false</testCases>
<serverSide>true</serverSide>
<subPackageByFileName>false</subPackageByFileName>
</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
Any ideas as to why this isnt generating the java code?
After taken a look into your pom I realized your problem. It's not related to calling mvn its based on the configuration you made.
You have configured the axistools-maven-plugin in the pluginManagement area. In this case you need to do this in the build area like this:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
..
</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
instead of:
<build>
<pluginManagement>
<plugins>
...
</plugins>
</pluginManagement>
...
</build>
If you configure it correctly you can use mvn clean package or mvn clean install instead of calling mvn axistools:wsdl2java ...

Resources