Maven install not respecting command line arguments - java

I have a project which I am attempting to install with maven. The pom.xml has a few properties in it which are modified when the maven install command is run depending on whatever version of a library we are attempting to build with:
<properties>
<some-version>0</some-version>
</properties>
The zero here is a placeholder, as we'll always specify a legitimate version during our build process. The version is then referenced later in the pom.xml to specify a few dependencies:
<dependencies>
<dependency>
<groupId>com.mycompany.myproduct</groupId>
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
</dependencies
Building is done via make with the following commandline:
mvn -Dsome-version=1.6.2
Maven is able to correctly resolve the version and build as expected. However, the version being installed in my local maven repository (/home/user/.m2) doesn't have the correct version. The pom.xml that is installed does not have the updated version I set in the command line:
user#ubuntu:~/$ cat /home/user/.m2/repository/com/mycompany/myproduct/myproject/1.0.0/myproject-1.0.0.pom | grep some-version -C 1
<properties>
<some-version>0</some-version>
</properties>
--
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
user#ubuntu:~/$
This is preventing any other project which depends on myproject from being able to build, as maven will complain that it can't find version 0 of someOtherProject:
[ERROR] Failed to execute goal on project myproject:
Could not resolve dependencies for project mycompany.myproduct:myproject:jar:1.0.0:
The following artifacts could not be resolved: com.mycompany.myproduct:someOtherProject:jar:0,
Could not find artifact com.mycompany.myproduct:someOtherProject:jar:0 in central (https://mycompany.com/artifactory/repo/) -> [Help 1]
What do I need to do for maven to install with the updated version in the pom? Obviously a terrible hackish solution would be to use sed and modify the pom file directly, but it seems that Maven should be able to actually leverage the command line settings when installing the pom. Otherwise the ability to set arguments on the command line seems remarkably limited in effectiveness.

Better you may set your property in pom.xml in <properties> tag like this -
<properties>
<property>
<name>some-version</name>
<value>1.6.2</value>
</property>
</properties>
If you use this then you don't have to provide the property each time you issue a mvn command from terminal.

mvn -Dsome-version=1.6.2 works as a substitution value for the scope of building than replacing the original POM with the new values. Hence is the behavior you see. I am not aware of any maven support to do so.

Under #JoopEggen's advice, I looked deeper into the maven versions plugin. It offered an update-property target which will actually update the pom.xml value on disk, rather than just passing in an overwrite during the build phase. I was able to solve my issue by calling
mvn versions:update-property -Dproperty=some-version -DnewVersion=1.6.2 -DsearchReactor=false -DallowSnapshots=true
in the makefile before calling mvn install. Disabling the reactor was necessary to prevent the plugin from rejecting values it couldn't find in the remote repo (see here), and allowSnapshots allows me to use version numbers such as 1.6.2-SNAPSHOT, useful when testing.

Related

Maven surefire-plugin different version at execution than in pom

I am trying to move from JDK 8 --> JDK 11 and sure enough maven surefire-plugin is failing. So I know that I need to change it's version to 2.21.0 or above that and I already did so. The version is only referenced in one pom so everything should be fine and even intellIJ says that I am using version 2.22.2.
The thing is I am when I run a mvn clean install -U or mvn test, or a mvn clean then mvn compile-test and lastly a test I am still getting error referencing:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test (default-test) on project modular-index-composer: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test failed.: NullPointerException
How is it even possible? I am 100% sure that the project must have a version of 2.22.2. Is there any other way where it could be set?
Kinda obvious/dumb solution:
So the project is made up of multiple modules, and there is a "parent" module that has most of the dependencies with their versions. I changed the sunfire version in this modules pom, and nothing seemed to be changing. (Eventhough intellij showed the correct newer version for the plugin)
So I decided to just exclude the parent pom and add the sunfire plugin with its version directly into my module. And behold it worked.
The issue is probably something with the parent pom, my guess would be that my module is not pulling from the correct parent pom version or it mixed it up somehow.

Downloading artifacts with all classifiers into a chosen directory

We have a private repository at work so once a while we need to download artifacts from the central repository and upload them.
Someone wrote a program a while ago for that which didn't always work perfectly: it would download all dependencies for each artifact separately, it would first scan the dependency tree and only then download them, it wouldn't download sources and javadocs, was slow, etc.
Recently I have come across this useful maven plugin called dependency and its goal get.
So now to download some artifact, let's say for instance com.sparkjava.spark-core:2.5.4, I would type on my terminal:
mvn dependency:get -Dartifact=com.sparkjava:spark-core:2.5.4
mvn dependency:get -Dartifact=com.sparkjava:spark-core:2.5.4:sources
mvn dependency:get -Dartifact=com.sparkjava:spark-core:2.5.4:javadoc
Which would download the artifact along with all its dependencies to the directory ~/.m2/repository which I can later transfer to our private repository.
This method works good but has 2 problems:
I have to run the command 3 times when each time the classifier is different and I would like to get all classifiers at once.
It always downloads the artifacts into ~/.m2/repository, which can sometime contain other artifacts I'm not intrested in having on the private repository. I need be able to choose another directory destination.
What can I do about these issues? About the first one I can work it around with an alias (which will still run the same command 3 times) but I have no idea what to do about the second issue and it's not mentioned on the plugin's documentation either.
To solve (2), you can update the settings.xml with a tag
<localRepository>C:\Users\my\custom\existing\directory</localRepository>
and make sure when you are building the project, the same settings.xml looked into which can be done using
mvn clean install -gs <pathToDirectory>
Note - Please modify the path accordingly.
To solve (1), adding the following to settings.xml should work as well -
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
Source - Maven – Always download sources and javadocs
Other maven command that could help here is -
resolve command with classifier :
mvn dependency:resolve -Dclassifier=javadoc #downloads all the documentation

Can I remove jar from local repository without pom.xml?

I install my jar to local repository by command:
mvn install:install-file -Dfile=aaa.jar -DgroupId=bbb -DartifactId=ccc -Dversion=1.0 -Dpackaging=jar
Now I want remove it from repository. I try command:
mvn dependency:purge-local-repository -DmanualInclude=bbb-ccc
But get error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:purge-local-repository (default-cli): Goal requires a project to execute but there is no POM in this directory (...). Please verify you invoked Maven from the correct directory. -> [Help 1]
Then I create pom.xml with data:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>bbb</groupId>
<artifactId>ccc</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>${project.packaging}</packaging>
<file>aaa.jar</file>
</properties>
</project>
Now I can remove jar from repository. But can I remove by command line without creating pom.xml?
I'm using 3.2.5 (windows x64).
The dependency:purge-local-repository goal, up to the current version 2.10, needs to be executed on a Maven project:
Requires a Maven project to be executed.
One of the purpose of this goal is to remove from the local repository the dependencies of a Maven project, so it needs one to execute. This explains the error you have.
However, with this plugin, it is possible to specify a manualInclude parameter, which will remove any dependency specified as groupId:artifactId:version or all versions of groupId:artifactId or even everything under a groupId. Therefore, the plugin could be updated to not require a Maven project to be executed.
I went ahead, created the JIRA issue MDEP-537 to track this addition and will fix this for version 3.0.0.
As Tunaki mentioned in his answer, he requested MDEP-537 for the Dependency plugin to be able to purge without a Maven project. This has been implemented since version 3.0.0.
If you only run mvn dependency:purge-local-repository, you are likely to run an older version. E. g. my Maven 3.6.0 comes with Dependency plugin version 2.8.0.
So you should run a specific version of the plugin:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.0:purge-local-repository -DmanualInclude=...

How do I build my Maven pom in Eclipse?

I have a project that uses UI4J, and instead of using external jar I decided to go for maven, I am going to distribute it via git, so I guessed that this is a much better approach.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Kalamaria</groupId>
<artifactId>KalamariaHarverst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenFirst</name>
<dependencies>
<dependency>
<groupId>com.ui4j</groupId>
<artifactId>ui4j</artifactId>
<version>2.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
The problem is that I can't find a way to get this to work. What ever I try I am still getting errors on the import of the library, meaning that the jar of the ui4j is not imported.
I have (among others) tried to do a "Maven bumvn eclipse:eclipseild" with "clean install" as goal
Downloading source and Updating Project from the Maven menu
even tried to do a mvn eclipse:eclipse from the console, but i got this error
The program 'mvn' can be found in the following packages: * maven *
maven2 Try: sudo apt-get install
How does this work, what should I do to import the declared jars?
Removing <type></type> was the correct first step that you need to do.
Now, there's probably still a pom.lastUpdated file in your repository that is wrong, you need to forcibly override it. The easiest thing to do is just delete the entire directory in your .m2 directory, which is located in your OS dependent home directory. On windows, this would be:
C:\Users\<username>\.m2\repository\com\ui4j
On Linux, this is usually in:
/home/<username>/.m2/repository/com/ui4j
Delete that directory, and then do Maven -> Update project, this should fix your problem.
By the way, mvn eclipse:eclipse is almost never the right thing to do, it's much better to use m2eclipse for your eclipse integrations as it works much more seamlessly.
I am not able to reproduce the behavior of the pom type being added automatically when you add the ui4j dependency. However, most of the time the correct dependency <type> is jar, as that is the default. pom dependencies are most often used when a project is simply a pom and nothing else, which is common as the parent pom of an entire application.
In this case (as in most cases), the type you want is jar, so don't specify a type parameter.
To add a bit more background to #durron597 's correct answer:
tag defines what Maven looks for in the repository when it downloads your artifact. Various types exist. Most common and default is "jar" - Maven will look for something packaged as "jar" - a regular *.jar file. Other types include "test-jar", "war" and "pom". Type "pom" means that Maven will look for something packaged as "pom" - basically your dependency's pom.xml file. Most of the artifacts you refer to are packaged as "jar" and do not supply "pom" packaging. See http://maven.apache.org/pom.html#Dependencies, search for "Type".

Maven 3.3 override version

I am using Maven 3.3.3. I just upgraded from 3.1.1 and noticed that I can't pass in a version anymore.
pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.hello.world</groupId>
<artifactId>helloworld</artifactId>
<version>${VERSION_NUMBER}</version>
<packaging>pom</packaging>
<properties>
<VERSION_NUMBER>LOCAL-ONLY-SNAPSHOT</VERSION_NUMBER>
</properties>
mvn package -DVERSION_NUMBER=1.2.3
After upgrading to Maven 3.3.3, I now get the error message:
[ERROR] Version must be a constant
My goal is to be able to pass in a version number and never have actual numbers in the pom.xml. I don't want to use the versions-plugin, as that actually changes the pom.xml to use a specific version number. No version numbers in source control.
Unfortunately, it is not possible to do what you're attempting in the latest versions of maven - the version is so integral to the build process that you must specify it explicitly in your POM.
The only way around it (and I'm not suggesting that this is a good idea) would be to add a pre-processing step before your maven command e.g. using sed to modify the POM and set whatever version number you want.
Just out of curiosity, why do you want to do this?
You have to use one or a combination of the following properties:
${revision}, ${changelist}, and ${sha1}.
That's simply the reason it does not work.

Categories

Resources