Can I store a maven command in the pom? - maven

Sometimes I need to run custom maven commands, e.g:
mvn install -DskipTests -DfailIfNoTests=false
I would like to be able to save this command so I don't have to type it out every time, e.g.
mvn forceInstall
Of course I can create run configurations in my IDE but I'm wondering if this is possible using maven only.

In the pom.xml you can define a default goal like this:
<project>
<build>
<defaultGoal>install</defaultGoal>
...
</build>
</project>
so you can use simply mvn only instead of mvn install. But for your need it would be better to use the .mvn/maven.config file which will be part of your project which can contain any kind of Maven commands. So you create a file in .mvn/maven.config which contains:
-DskipTests
-DfailIfNoTests=false
This can be used starting with Maven 3.3.1+.
Apart from that I would really reconsider using -DskipTests as default which from my experience only makes occasionally sense. What makes often sense is to use things like -T 4 etc. in the .mvn/maven.config file...or --fail-at-end. Be careful that those options file is not taken into account by CI server like Jenkins etc.

Related

Setting the developerConnection for the Maven Release Plugin from the command line

I am currently trying to configure the Maven Release Plugin for our build server.
For that I am trying to set the <scm><developerConnection> through the command line. I read that
project.scm.developerConnection
is the command line property(https://maven.apache.org/guides/mini/guide-releasing.html). I tried to set it but it seems to have no effect. When I start the build, it uses a constructed URL (parent pom url + artifactId) that fails.
I have looked at the source code of the plugin but did not find the command line property mentioned above.
Can anybody shed light on this?
It looks that you cannot pass this property directly from command line. See:
https://issues.apache.org/jira/browse/MRELEASE-707
But you should get it working by specifying it through a custom property in your pom.xml:
<properties>
<my.developer.connection />
</properties>
<scm>
<developerConnection>${my.developer.connection}</developerConnection>
<tag>HEAD</tag>
</scm>
And running maven with, for example:
-Dmy.developer.connection=scm:git:ssh://user#host/repo.git
I use this approach to keep my pom.xml clean when generating a public release that should not contain information about my company's internals.
When you run mvn release:prepare, Maven forks. The arguments supplied on the command line are passed to the initial Maven call (the one you/build server ran) not to the fork.
To pass args to the release plugin, supply the arguments as shown:
mvn release:prepare -Darguments="-Dproject.scm.developerConnection=..." ...
Depending on what I'm trying to do, sometimes I've had to specify in two places, so both original and forked processes get the args:
mvn release:prepare -DsomeArg=val -Darguments="-DsomeArg=val" ...
The first example in the release plugin FAQ shows an example of where the latter is useful.
---- Update ----
I found the property in the maven-scm-plugin code.
Maybe project.scm.developerConnection is read-only? Try setting scmDeveloperConnection instead, as it's listed as the property name.

Maven: Define SuperPOM property project.build.directory over command line

I am building my maven project with GitLab CI on a docker file.
I would like to configure my pipeline with a "compile" stage and a "test" stage. To be able to do that, I need to set the property project.build.directory, which is defined in the maven super POM, to the docker cache so the compiled artefact does not get lost between the jobs.
project.build.directory is a predefined maven property. Therefore I would think that I am able define it with the CL parameter -Dproject.build.directory=anotherDir. This somehow does not work and my project still gets built to the default directory target.
If I modify my POM with
<properties>
<buildDir>target</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
and call mvn clean install -DbuildDir=customTargetDir, my project gets built to the customTargetDir as expected.
Why is that? I really don't see a difference. I both cases, I define the value of an existing property.

how do I get maven to tell me what jar it would build without building it?

Given a particular pom.xml, maven will build a given package based on the settings.
Is there any way on the command-line I can ask maven to tell me what output file it would build, without actually building?
E.g., if part of my pom.xml is
<artifactId>foo</artifactId>
<version>0.2.5</version>
<packaging>jar</packaging>
I would want to do something like mvn package:tell-me-name-without-doing-anything and get an output like target/foo-0.2.5.jar

how to make a jar in maven? [duplicate]

This question already has answers here:
Making Maven run all tests, even when some fail
(5 answers)
Closed 3 years ago.
Actuality when i run tests they fails but i need to run them to get some .class files which are very important for my jar.
By default when test results fails , the jar is not build , could i add a setting in pom.xml which ignore that, so I can build the jar ignoring results from tests ?
I read something about "Maven Surefire Plugin" but I don't know how to use it...
Please refer to surefire:test for details, but the most useful properties are:
-Dmaven.test.failure.ignore=true (or -DtestFailureIgnore=true) - will ignore any failures occurred during test execution
-Dmaven.test.error.ignore=true ( deprecated ) - will ignore any errors occurred during test execution
-DskipTests - would compile the test classes but skip test execution entirely
-Dmaven.test.skip=true - would not even compile the tests
I believe that in your case where you want to compile test classes but not fail the build due to any tests errors and still create the jar.
You should use the first option to ignore any test failures which you can still review once the build has finished.
mvn -Dmaven.test.skip=true package skips the surefire test mojo.
to ignore test failures and keep maven from stopping you can add this to the section of the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
The solution is:
mvn -fn clean install
execute mvn --help for advanced options
Here's the excerpt for -fn
-fn,--fail-never NEVER fail the build, regardless
of project result
<properties>
<maven.test.skip>true</maven.test.skip>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>
http://jira.codehaus.org/browse/SUREFIRE-319
Or from command line
http://maven.apache.org/maven-1.x/plugins/test/properties.html
maven.test.error.ignore Yes Set
this to true to ignore errors during
testing. Its use is NOT RECOMMENDED,
but quite convenient on occasion
Use -DskipTests=true instead of -Dmaven.test.skip=true in order to skip tests but compile them.
Using -Dmaven.test.failure.ignore=true will also work but is not very nice.
Use the maven option -Dmaven.test.skip=true
E.g.
mvn package -Dmaven.test.skip=true

How to distribute a binary dependency in maven?

I'm trying to convert a project from ant to maven.
The unit tests depend on a third party binary jar, which is not available in any public maven repositories.
How do I make maven handle this situation? I have found two solutions, neither of which are acceptable. First is to use a system dependency; this doesn't work because a) the dependency should only be for the tests, and b) the dependency is not found by eclipse after generating an eclipse project.
Second is to manually install the dependency in a local repository. This seems to be the recommended way. I don't want to do this because I want users to be able to build and test with a simple 'mvn test'. If users have to read a document and copy/paste some shell commands to be able to build and test, then something's wrong.
I suppose it would be OK if maven itself installed the dependency in the local repository as part of the build - is this possible, and if so, how?
Aled.
You may want to look at install:install-file. You can make it execute in the early phase of your project (validate or initialize) via standard means.
On the second thought, if it fails because of missing dependency in the same project, there are couple more options. One is to call ant script via antrun plugin to install artifact.
Or create additional module not dependent on your artifact to be executed prior to main module and have that module install artifact as described earlier.
First of all my way would be using a repository manager such as nexus and installing this dependency to there.
However there is another solution. You can include this 3rd party jar to your project and with test plugin you can configure to include it in classpath such this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
<additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
By the way, I hope that you are aware of that maven is executing surefire plugin in order to run tests by default lifecycle.

Resources