Maven Versions Plugin - Skip running maven versions plugin during mvn build install - java

Whenever I run mvn clean install I want mvn versions plugin to run, which is how my project currently operates. This is perfect, but sometimes I may want to NOT run maven versions plugin to speed up build time (this case is more the exception that the rule).
But I can't find anyway to skip it... (there's no -DskipVersions=true AFAIK).
Does anyone know how can I skip the execution of only this plugin?
maven-versions-plugin: https://www.mojohaus.org/versions-maven-plugin/

Use a maven profile for executing the version maven plugin. When activeByDefault you do not need to add the profile for maven build.
<profiles>
<profile>
<id>useVersion</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<plugins>
<plugin>
...
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
...
</plugin>
</plugins>
</profile>
</profiles>
You could disable the profile by using !
mvn clean install -P !useVersion
For further information Llook at https://maven.apache.org/guides/introduction/introduction-to-profiles.html

Related

IntelliJ resolve dependencies of profile

I have a Java project with multiple modules. In this project, one module has a dependency to another one, which is only needed for a specific profile and hence is defined like that:
<profile>
<id>myProfile</id>
<dependencies>
<dependency>
<groupId>MyGroupId</groupId>
<!-- ... -->
</dependency>
</dependencies>
<!-- ... -->
</profile>
This works fine when building manually with maven like that:
mvn clean install -P myProfile
When using the IntelliJ build however, the dependency doesn't get resolved.
I've tried the option to delegate IDE build/run actions to maven, adding a property for maven in Build, Execution, Deployment > Build Tools > Maven > Runner (namely -P -> myProfile), and much more which is most likely not of interest.
Is it possible to configure IntelliJ to resolve the dependencies for a specific profile?
To help Intellij Idea to understand about your maven profile and maven object mode, you set as default profile in maven so that by default it will be recognized and run by any IDE. I provide below the code snippet.
<profile>
<id>firstProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
..... Other code goes
</profile>
So inside profile, use this <activeByDefault>true</activeByDefault>. It may solve the problem.

Maven release plugin skipping tests on perform but not prepare

When using the maven release plugin, both mvn release:prepare and mvn release:perform will run the tests.
Is there any way I can configure things so they only run during mvn release:prepare?
I know you can pass:
-Darguments="-DskipTests"
But this will skip the tests during both goals, which I don't want. And I also don't want them to run during just mvn release:perform and not prepare, as a failure during perform will have already tagged the repository.
You should be able to do that by adding the <releaseProfiles> element to the release-plugin configuration. This will only be used for release:perform Mojo.
Something like this should do the trick:
<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<releaseProfiles>skipTestsForReleasePerform</releaseProfiles>
</configuration>
</plugin>
</plugins>
</build>
(...)
<profiles>
<profile>
<id>skipTestsForReleasePerform</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Maven – Always download sources and javadocs

Is there a way I can configure maven to always download sources and javadocs? Specifying -DdownloadSources=true -DdownloadJavadocs=true everytime (which usually goes along with running mvn compile twice because I forgot the first time) becomes rather tedious.
Open your settings.xml file ~/.m2/settings.xml (create it if it doesn't exist). Add a section with the properties added. Then make sure the activeProfiles includes the new profile.
<settings>
<!-- ... other settings here ... -->
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
</settings>
Edit: As mentioned by Jingguo Yao, this works with Eclipse IDE only - the same can also be configured in your IDE of choice. In Elcipse via Window -> Preferences -> Maven menu, though this probably has to done at every workspace level and for fresh Eclipse installations.
Alternatively configure the maven-dependency-plugin in your pom.xml in a separate profile and run it as required - keeping it in the main build will lead to build times (needlessly elongating (not to mention space) at places like your build nodes that don't need either sources or java docs. Preferable this should configured in some org or division parent pom.xml, otherwise it has be repeated everywhere in different places
In my case the "settings.xml" solution didn't work so I use this command in order to download all the sources:
mvn dependency:sources
You also can use it with other maven commands, for example:
mvn clean install dependency:sources -Dmaven.test.skip=true
To download all documentation, use the following command:
mvn dependency:resolve -Dclassifier=javadoc
Just consolidating and prepared the single command to address source and docs download...
mvn dependency:sources dependency:resolve -Dclassifier=javadoc
Answer for people from Google
In Eclipse you can manually download javadoc and sources.
To do that, right click on the project and use
Maven -> Download JavaDoc
Maven -> Download Sources
I am using Maven 3.3.3 and cannot get the default profile to work in a user or global settings.xml file.
As a workaround, you may also add an additional build plugin to your pom.xml file.
<properties>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
</properties>
<build>
<plugins>
<!-- Download Java source JARs. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
On NetBeans :
open your project explorer->Dependencies->[file.jar] rightclick->Download Javadoc
As #xecaps12 said, the simplest/efficient approach is to change your Maven settings file (~/.m2/settings.xml) but if it is a default settings for you, you can also set it like that
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
In Netbeans, you can instruct Maven to check javadoc on every project open :
Tools | Options | Java icon | Maven tab | Dependencies category | Check Javadoc drop down set to Every Project Open.
Close and reopen Netbeans and you will see Maven download javadocs in the status bar.
To follow up on the answer from kevinarpe this does both sources and Javadocs:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>sources</goal>
<goal>resolve</goal>
</goals>
</execution>
</executions>
<configuration>
<classifier>javadoc</classifier>
</configuration>
</plugin>
</plugins>
</build>
I think it can be done per plugin. See this chapter from the Maven book.
You might be able to configure the dependency plugin to download sources (even though I haven't tried it myself :-).
Simply modify file mvn (or mvn.cmd if in windows) and add whatever command line switches you need (as mentioned by other answers). If you don't want to modify the install files (which I'd recommend), create a mymvn (or mymvn.cmd) wrapper that invokes the regular mvn with the parameters.
Not sure, but you should be able to do something by setting a default active profile in your settings.xml
See
See http://maven.apache.org/guides/introduction/introduction-to-profiles.html
I had to use KeyStore to Download the Jars. If you have any Certificate related issues you can use this approach:
mvn clean install dependency:sources -Dmaven.test.skip=true -Djavax.net.ssl.trustStore="Path_To_Your_KeyStore"
If you want to know how to create KeyStores, this is a very good link:
Problems using Maven and SSL behind proxy
For the sources on dependency level ( pom.xml) you can add :
<classifier>sources</classifier>
For intellij users, inside the pom.xml file, right click anywhere and select Maven -> Download sources and Documentation.

Maven offline - problem with mvn-plugins

I'm using maven in my project and I need to run the build in a non-internet-access machine.
When I test my project build everything is working, but when I run the build in a future moment, the maven try to update the mvn-plugins and this sht* is broking my build.
My config file: settings.xml from mvn.
<profile>
<id>blaProfile</id>
<repositories>
<repository>
<id>blaRepo</id>
<url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>blaRepo</id>
<url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>blaProfile</activeProfile>
</activeProfiles>
And I ran my maven is with the params:
mvn -npu -bla.3rdParty.home="$(THE_CORRECT_PATH)" package
I saw that maven try to update some mvn-plugins for some time, but the option:
-npu,--no-plugin-updates Suppress upToDate check for any relevant
Should work for this updates.
Well waiting some help on that!
Thanks in advance!
UPDATE(1):
What I'm looking at, is that I could use the setting:
<usePluginRegistry>true</usePluginRegistry>
Inside my settings.xml and with this, I'll have a plugin-registry.xml inside ${user.home}/.m2 that I can config and force the maven plugins versions.
But it's not working! :(
Before you go offline run the following:
mvn dependency:go-offline
That will download all your dependencies and plugins that you need to build your project into ~/.m2/repository.
Once you've run that you can now build your project offline using the '-o' flag:
mvn install -o
In order to cache plugins into the .m2/repository folder you would need to specify all plugins explicitly with the mvn <maven-plugin-name>:help
You would also need to specify explicit version for each plugin in the <plugins> or <pluginsManagement> section of your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
</plugin>
This is needed to make sure that mvn install -o uses the same plugin version.
Ofcourse you would also need to run mvn dependency:go-offline to take care of your compile and test dependencies.
mvn assembly:help compiler:help enforcer:help exec:help failsafe:help install:help jar:help resources:help surefire:help
mvn dependency:go-offline
mvn compile --offline
Maven Go-offline + Isolated Docker Multi-stage Image Builds
My answer is for both a local build or a Dockerized environment, which is isolated on the nature of how docker images are built. This uses Maven 3.6.3-jdk-8.
With this answer, you know exactly how much time your CI spends on downloading, compiling, testing, packaging...
Finally, also answering to the old question on Jira for the go-offline https://issues.apache.org/jira/browse/MDEP-82?focusedCommentId=16997793&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16997793
Update pom.xml dependencies
maven-dependency-plugin
surefire-junit-platform
Call go-offline resolving dependencies
Call any mvn command with the switch --off-line
Set latest versions of the plugins
## -23,6 +23,9 ##
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<common-io.version>2.6</common-io.version>
<jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
+ <!-- https://issues.apache.org/jira/browse/MDEP-82 -->
+ <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
+ <surefire-junit-platform.version>2.22.2</surefire-junit-platform.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-surefire-report-plugin.version>2.22.2</maven-surefire-report-plugin.version>
...
...
<build>
<plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>${maven-dependency-plugin.version}</version>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
## -135,6 +143,11 ##
<target>${java.version}</target>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.surefire</groupId>
+ <artifactId>surefire-junit-platform</artifactId>
+ <version>${surefire-junit-platform.version}</version>
+ </plugin>
Create the Go-offline cache
This will ensure all the dependencies are downloaded
More than 3m to download all dependencies
FROM maven:3.6.3-jdk-8 AS dependencies-downloaded
...
...
COPY pom.xml /usr/src/app/pom.xml
COPY settings.xml /usr/src/app/settings.xml
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml dependency:resolve-plugins dependency:go-offline
Call compile with --offline
We can reuse the same image for compilation
Only takes 7s because nothing is downloaded
FROM dependencies-downloaded AS compile
COPY app /usr/src/app
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml compile --offline
Call tests with --offline
We can reuse the same image for tests
Taking 18s to run the test cases, without any download whatsoever
FROM compile AS tests
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml test --offline
Call package with --offline
We can reuse the same image for the final jar
Skipping even the tests ran in the previous docker image
Taking way less than before
FROM tests AS package
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml package -Dmaven.test.skip=true --offline
The final runtime image is a Docker image from the package.
FROM JRE
COPY --from package /usr/src/app/target /bin
...
...
It should suffice to run a mvn clean install on your code. The next time, when you run it in an offline mode you can use the following options:
mvn -Dmaven.repo.local=..\repository –o clean install
-o tells Maven not to try to update its dependencies over the network and with -Dmaven.repo.local you provide the path of the repository which contains all the required dependencies.
Alternatively, you can add the path of the repository in your settings.xml in the localRepository tag and add an <offline>true</offline>.
You can configure the same in your Maven Eclipse configurations.
After some debugging I found that maven-dependency-plugin (version 3.1.1 at the time of writing) is unable to resolve plugin's dependencies when specified like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
<dependency> <--- this is not going to be resolved by dependency:go-offline command !!!
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>3.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
After that I found go-offline-maven-plugin which just works! Pls see https://github.com/qaware/go-offline-maven-plugin for more info.
<plugin>
<groupId>de.qaware.maven</groupId>
<artifactId>go-offline-maven-plugin</artifactId>
<version>x.y.z</version>
</plugin>
Current version could be found here https://mvnrepository.com/artifact/de.qaware.maven/go-offline-maven-plugin and Maven issue here https://issues.apache.org/jira/browse/MDEP-82
I think this happens because Maven hasn't got the metadata available locally to determine if its plugin versions are correct. If you specify exact versions for your plugins (which is a good idea for reproducability anyway), it doesn't need to do the check, so will not try to connect to the remote repositories.
By specify exact versions, I mean that in your project's POM you should add the version to the plugin declaration. For example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- set the version explicitly-->
<version>2.0</version>
</plugin>
</plugins>
Note you can also force Maven to use internal repositories instead of Central by setting up repository mirrors. See this answer for more details on using repository managers and mirrors.
In the config you included, you're setting your remote repository to point to your local repository, this is not a good idea. To run offline you should either pass -o at the command line or add this to your settings.xml:
<offline>true</offline>

Maven Jdepend report contains no data

I'm running the jdepend maven plugin on my project and whether I run "mvn site:site" or "mvn jdepend:generate" the report that gets generated says "There are no package used." There are no errors in the maven output. Other plugins (cobertura, findbugs, etc.) run fine. My pom is configured like this:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
</plugin>
Any ideas?
Did you try running "mvn -U -cpu site:site" to update all the maven dependencies?
Maybe this question is better asked in the Maven forum :)

Categories

Resources