Injecting a variable into pom.xml from Jenkinsfile - maven

I am trying to configure SCM for my maven release plugin. However, the issue is: the pom.xml in my archetype, I dont want to hardcode a single scm variable but rather a parametrized version. In my jenkinsfile, we have a proprietary tool that fills the scm url, so I want to substitute that url into my pom.xml once I setup the project from my archetype. See code snippet below.
<developerConnection>scm:git:${myVariable}</developerConnection>
In my Jenkinsfile, I pull out this value like this (this works correctly):
node {
stage 'Checkout'
checkout scm
def myVariable= sh(returnStdout: true, script: 'git config remote.origin.url').trim()
So, to recap, I want to know how the value from Jenkins can get substituted into the pom.xml?
Right now, I am getting this error:
[INFO] fetch url: ${myVariable}
[INFO] push url: ${myVariable}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.755 s
[INFO] Finished at: 2017-07-17T16:21:00+00:00
[INFO] Final Memory: 20M/784M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project newSeedService: An error is occurred in the checkin process: Exception while executing SCM command. JGit checkin failure! ${myVariable}: not found. -> [Help 1]
Thank you

if you want to isolate these properties from the project POM, you can use properties file, you will have to use Properties Maven plugin, and run it's read-project-properties goal in the initialize phase of the Maven lifecycle. The example from the plugin page is reproduced here:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>etc/config/dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Related

The parameters 'url' for goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file are missing or invalid

I'm using the maven-deploy-plugin to deploy into Nexus an artifact generated by a task of maven-antrun-plugin at the end of my build.
The Ant task simply creates a file .sh of the assembled archive built by Maven.
I had to include maven-deploy-plugin because otherwise the .sh is not uploaded into Nexus and it's completely ignored by the lifecycle of the build.
This is the plugin configuration I've tried:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>deploy-sh</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<skip>false</skip>
<file>${project.build.directory}/${project.artifactId}-${project.version}-autoinstaller.sh</file>
<repositoryId>myrepo</repositoryId>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</configuration>
</execution>
</executions>
</plugin>
I run the Maven build with mvn clean deploy goals, but it fails with the following error:
[INFO] --- maven-deploy-plugin:3.0.0-M1:deploy-file (deploy-sh) # MyApplication ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:11 min
[INFO] Finished at: 2020-05-01T19:07:40+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file (deploy-sh) on project MyApplication: The parameters 'url' for goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file are missing or invalid -> [Help 1]
The parameters 'url' for goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file are missing or invalid
Yes, this occurs because there's not the <url> tag in the section, so it's a mandatory parameter.
But why the <distributionManagement> is completely ignored? I also have a distributionManagement configured for myrepo repository, so urls are configured there.
What I have to do to make it work within the distributionManagement for snapshots and releases?
Following this answer, I found the following solution:
I have removed completely the maven-deploy-plugin
I have added <attachartifact> ant task in the maven-antrun-plugin
In this way, the file produced by the Ant run plugin is correctly deployed to Nexus, as described here:
AttachArtifact Task
This task will attatch an artifact to the current Maven project. This is can be used to install and deploy an artifact generated by the Ant tasks.

How can I launch "bower install" then "spring-boot:run" using Maven?

I'm a studdent and I does not understand something with Maven in my project.
I use spring-boot and angular 1 within the same repository and I need a solution to first execute a "bower install" before using the maven pluggin "spring-boot:run".
I'd to know if it's possible to customize the maven command.
I use IntelliJ and all I do is pressing the start button in my main class
package fr.studionline;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#EnableAutoConfiguration
public class StudionlineBackApplication {
public static void main(String[] args) {
SpringApplication.run(StudionlineBackApplication.class, args);
}
}
The bower_components directory is in another directory than the main class as described in the picture below :
tree view of my project
Thanks in advance if you can help me understand how it work.
I will respond to any questions if there is something missing in my question.
UPDATE:
I did try the front-end-maven pluggin with this pom configuration :
<build>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- Use the latest released version:
https://repo1.maven.org/maven2/com/github/eirslett/frontend-maven-plugin/ -->
<version>1.4</version>
<executions>
<execution>
<id>bower install</id>
<goals>
<goal>bower</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
</executions>
<configuration>
<workingDirectory>src/main/resources/static</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
and this was what happend when I execute the command frontend:bower
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building studionline-back 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- frontend-maven-plugin:1.4:bower (default-cli) # studionline-back ---
[INFO] Running 'bower install' in C:\Antoine\Ecole\Projet\ProjetAnu_5A\studionline-back\src\main\resources\static
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.071 s
[INFO] Finished at: 2017-07-06T17:59:58+02:00
[INFO] Final Memory: 11M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.eirslett:frontend-maven-
plugin:1.4:bower
(default-cli) on project studionline-back: Failed to run task: 'bower install' failed.
java.io.IOException:
Cannot run program "C:\Antoine\Ecole\Projet\ProjetAnu_5A\studionline-back\src\main\resources\static\node\node.exe"
(in directory "C:\Antoine\Ecole\Projet\ProjetAnu_5A\studionline-back\src\main\resources\static"):
CreateProcess error=2, Le fichier spécifié est introuvable -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Process finished with exit code 1
But I still doesn't understand how to launch it with Intellij. Should I do mvn run instead of clicking on the "Run" button ?
Have a look at the frontend-maven-plugin. In particular it has a bower runner.
<execution>
<id>bower install</id>
<goals>
<goal>bower</goal>
</goals>
<configuration>
<!-- optional: The default argument is actually
"install", so unless you need to run some other bower command,
you can remove this whole <configuration> section.
-->
<arguments>install</arguments>
</configuration>
</execution>
Check the "Optional Configuration" section to see how to configure your front end directory. In your case:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- optional -->
<configuration>
<workingDirectory>src/main/resources/static</workingDirectory>
</configuration>
</plugin>

Maven Invoker Plugin not detecting failed test

As part of a root parent pom project, several integration tests have been added to test it on sample projects.
The structure of the project folder is as following:
-root-maven-parent-project
|- src
| |-it
| |-sample-project-test1
| |-sample-project-test2
| |-sample-project-test3
| |-settings.xml
|- pom.xml
The main issue is: although the build of sample-project-test2 is wrongly failing (it should not), the build is SUCCESSFUL for the Invoker plugin and the overall build does not fail.
Here is the concerned maven-invoker-plugin configuration:
<profile>
<id>it-tests</id>
<build>
<plugins>
<!-- Integration tests configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<streamLogs>true</streamLogs>
<goals>
<goal>clean</goal>
<goal>generate-sources</goal>
</goals>
<settingsFile>src/it/settings.xml</settingsFile>
<failIfNoProjects>true</failIfNoProjects>
</configuration>
<executions>
<execution>
<id>integration-test-release</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<cloneProjectsTo>${project.build.directory}/its/sample-project-test1</cloneProjectsTo>
<pom>src/it/sample-project-test1/pom.xml</pom>
<properties>
<scmBranch>release-something</scmBranch>
</properties>
</configuration>
</execution>
<execution>
<id>integration-test-hotfix</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<cloneProjectsTo>${project.build.directory}/its/sample-project-test2</cloneProjectsTo>
<pom>src/it/sample-project-test2/pom.xml</pom>
<properties>
<scmBranch>hotfix-something</scmBranch>
</properties>
</configuration>
</execution>
<execution>
<id>integration-test-master</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<cloneProjectsTo>${project.build.directory}/its/sample-project-test3</cloneProjectsTo>
<pom>src/it/sample-project-test3/pom.xml</pom>
<properties>
<scmBranch>master</scmBranch>
</properties>
</configuration>
</execution>
</plugin>
</plugins>
</build>
</profile>
As you can see, multiple executions are configured because each execution would need its own properties. Each execution is also pointing at its own integration test project and pom.
The build is clearly failing for a specific execution:
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD FAILURE
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 2.337 s
[INFO] [INFO] Finished at: 2017-07-04T17:35:49+02:00
[INFO] [INFO] Final Memory: 12M/220M
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-snapshot-management) on project cmp-sample-project-test2: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[INFO] [ERROR]
[INFO] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[INFO] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
[INFO] [ERROR]
[INFO] [ERROR] For more information about the errors and possible solutions, please read the following articles:
[INFO] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[INFO] pom.xml .......................................... FAILED (4.1 s)
[INFO] The build exited with code 1. See C:\data\git-repositories\root-maven-parent\target\its\sample-project-test2\build.log for details.
However, at the bottom of the build we see that the verify goal of the maven-invoker-plugin aggregated the results, flagged the concerned test as Passed and made the build SUCCESS:
[INFO]
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-release) # root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO]
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-hotfix) # root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO]
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-master) # root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Moreover, by only running the failing test from command line as:
mvn invoker:integration-test#integration-test-hotfix invoker:verify -Pit-tests
The sub-build of the test project fails, the output is correctly marked as Failed in the test summary, and the build is correctly ending with FAILURE.
Question: why when executing multiple integration tests using the maven-invoker-plugin, although a test is failed, it is marked as Passed in the test summary and the build does not fail, while running only the isolated test everything fails correctly?
Note: no invoker property file is used.
Issue solved with the following explanation, although I think something could be improved in the behavior of the plugin (see below).
The whole maven-invoker-plugin was reduced to the following configuration:
<profile>
<id>it-tests</id>
<build>
<plugins>
<!-- Integration tests configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<streamLogs>true</streamLogs>
<goals>
<goal>clean</goal>
<goal>generate-sources</goal>
</goals>
<settingsFile>src/it/settings.xml</settingsFile>
<failIfNoProjects>true</failIfNoProjects>
<cloneProjectsTo>${project.build.directory}/its</cloneProjectsTo>
</configuration>
<executions>
<execution>
<id>integration-test-release</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Basically: only one plugin execution, instead of an execution per test, which indeed was verbose and non scalable, but forced by the need of having different values for the same property in each integration test. Apparently, this is not possible via pom configuration and only achievable - unless I am not mistaken - via a test.properties file.
Hence, as a complement to the configuration above, I added in each and every integration test project folder a test.properties file with the following content e.g.:
scmBranch=master
De facto replacing what in the pom.xml file was (as part of an execution of the maven-invoker-plugin:
<properties>
<scmBranch>master</scmBranch>
</properties>
This mechanism (single execution of the plugin + test properties file per test folder) fixed the issue, allowing the build to have multiple integration tests each with its own different value for the same property. Hopefully this solution may help troubleshooting similar issues.
Here is the final result from the build correctly aggregating tests and effectively respecting their sub-build output (while before the build was generating 6 Build Summary of Passed: 1 each time, although not correct).
[INFO] --- maven-invoker-plugin:3.0.0:verify (pom-integration-test) # root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 6, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
However, some questions remain:
Without using the test.properties file, how to achieve the same via pom.xml configuration? Normally, it should only be an alternative, not a mandatory and only possible solution. That's why this is rather a uncomplete feature (a bug?) to me.
Having multiple execution of the plugin results in test summaries at the end of the build which correctly follow the executions order, the number of tests executed (always 1 per execution, in this case), but apparently do not reflect the effective result of each sub-build. Why? This is rather a bug or a misbehavior of the plugin due to an unexpected usage of it, perhaps.
use this configuration :-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0</version>
<configuration>
<rules>
<banDuplicateClasses>
<findAllDuplicates>true</findAllDuplicates>
</banDuplicateClasses>
</rules>
<fail>false</fail>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>extra-enforcer-rules</artifactId>
<version>1.0-alpha-1</version>
</dependency>
</dependencies>
</plugin>
for more refer this link :
http://maven.apache.org/enforcer/maven-enforcer-plugin/

Properly binding maven exec plugin to test phase

In a Maven project I use maven-exec-plugin to launch my Grunt tests. It goes like this:
<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>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>cmd</executable>
<workingDirectory>${project.basedir}/src/main/webapp</workingDirectory>
<arguments>
<argument>/C</argument>
<argument>grunt --no-color test</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
With that I can run mvn test and my grunt test task will be executed: if the tests pass, the maven build pass and if the tests fail the maven build fail. When some tests fail I have the following output:
............................................. 1 failing
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.496s
[INFO] Finished at: ** ** ** **:**:** CEST ****
[INFO] Final Memory: *M/*M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default) on project *****: Command execution failed. Process exited with an error: 6 (Exit value: 6) -> [Help 1]
I would like to know if it is possible to have the 'usual' maven output for failing tests. Something like: Build failure, there are failing tests.
Thanks
Not easily. The build failure message shows the exception thrown by the failing plugin; in exec-maven-plugin's case, that will only ever be the report of the exit code. It won't consider anything else in the command's behaviour.
If you were determined, you could rewrite or extend exec-maven-plugin, or perhaps write something similar in Groovy to throw an exception with a more specific message.
(A grunt-maven-plugin exists, but it also delegates to exec-maven-plugin.)

How can I specify an ant target to run during the release:prepare goal?

When using the maven release plugin I want to do some pre-work (via an ant tast) as part of the release build with the assurance that the same code base is used (so no commits sneak in between). I have an ant task that I want to call to do this, but I'm having the following issue:
inside my pom file:
<configuration>
<preparationGoals>antrun:run -Dtarget=${antTaskJarBuildXML} clean verify</preparationGoals>
</configuration>
where ${antTaskJarBuildXML} is:
<target><ant antfile=\"../build.xml\" target=\"iv_dependency_build\" /></target>
When I run release:perform this is the log:
...
[INFO] Not generating release POMs
[INFO] Executing goals 'antrun:run -Dtarget="<target><ant antfile=\"../build.xml\" target=\"iv_dependency_build\" /></target>" clean verify'...
[WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] [INFO] Scanning for projects...
[INFO] [WARNING]
[INFO] [WARNING] Some problems were encountered while building the effective model for com.xactsites:iv:war:12.12.4.9
[INFO] [WARNING] The expression ${version} is deprecated. Please use ${project.version} instead.
[INFO] [WARNING]
[INFO] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[INFO] [WARNING]
[INFO] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[INFO] [WARNING]
[INFO] [INFO]
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Building iv 12.12.4.9
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [WARNING] The artifact javax.xml:jaxrpc:jar:1.1 has been relocated to javax.xml:jaxrpc-api:jar:1.1
[INFO] [INFO]
[INFO] [INFO] --- maven-antrun-plugin:1.7:run (default-cli) # iv ---
[INFO] [INFO] No ant target defined - SKIPPED
[INFO] [INFO]
[INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) # iv ---
[INFO] [INFO] Deleting C:\dev\apps\iv\target
[INFO] [INFO]
...
And as shown in the log, I am told that no target is specified. I followed what I understood from the ant run documentation
Am I missing something in how to pass in the target name?
Is this the best approach?
Is it a matter of escaping that I am missing? I'm on windows and this is the actual value defined for the xml (${antTaskJarBuildXML}):
"<target><ant antfile=\"../build.xml\" target=\"iv_dependency_build\" /></target>"
EDIT
#carlspring has given some great feedback (+1 on his answer), however, due to the nature of the problem where not everything is mavenized I couldn't get this working. Maven is expecting to be in control of the whole release process, but I need to perform an ant task (which creates dependencies needed for the build in question) beforehand. I also need to be assured that this prework task and the regular build are built against the same git tag/hash. My current solution is to sequentially do the steps that the release plugin would perform as discussed here. Through this I can create a git tag then do the maven build against that same git tag. If there are any better ideas out there please contribute!
My suggestions would be for you to define a profile and have your ant-run definition in it.
The release plugin forks, meaning your command-line args will be ignored.
UPDATE:
Try this:
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>execute-prepare</id>
<!-- Set up your Ant stuff here -->
<goals>
<goal>prepare</goal>
</goals>
<configuration>
<!-- If you have args specific for your release, put them here: -->
<arguments>-Pant-run-release</arguments>
<releaseProfiles>ant-run-release</releaseProfiles>
<mavenExecutorId>forked-path</mavenExecutorId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>ant-run-release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>execute-something</id>
<!-- Set up your Ant stuff here -->
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

Resources