Using maven versions plugin to handle dependency version - java

I have the a project pom and a parent pom. The parent pom defines a dependency as follows:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mycode/groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
My project pom inherits the parent and defines the dependency.
<parent>
<groupId>au.com.truelocal</groupId>
<artifactId>truelocal-parent</artifactId>
<version>develop</version>
</parent>
<dependencies>
<dependency>
<groupId>com.mycode</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
I used to build with Maven this way without issue.
I now want to start having a different version in parent, common and project.
I am going to use the maven versions plugin to set my version.
I am setting the parent version and version successfully however, maven keeps looking for the wrong version of common. E.g. if my project is building as version 1.0.0-4 then it looks for common-1.0.0-4 but I actually need it to get the latest version of common instead which could be for example 1.0.0-23.
Can i use the versions plugin to adjust my dependency version? How do I make it only apply to common and not other dependencies I may have?

mvn versions:set (I guess this is what you've done) sets the version of your project, respectively the project versions which are part of a multi module project.
It doesn't touch the versions of the dependencies, either they are configured in a parent pom within <dependencyManagement> or within <dependencies>.
You can adjust the dependency versions by several ways: mvn versions:display-dependency-updates , mvn versions:use-latest-releases , mvn versions:use-latest-snapshots.
Use mvn versions:help or mvn versions:help -Ddetail=true -Dgoal=use-latest-snapshots to get more informations.

Related

How to inherit dependency version from required BOM POM in maven spring boot project?

I am new to spring boot world and encountered with an issue. I have a requirement of using mssql in my project which I am default getting as a dependency from spring-boot-starter-parent but the catch is the version which is getting inherited is what i do not want.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
</dependency>
This will give me mssql version mssql-jdbc : 6.1.0.jre7 (Which i do not want)
My organization has a dedicated BOM POM created for all the common components used in all the projects which includes mssql latest version. mssql-jdbc :8.4.1.jre8
I have declare this BOM POM under dependencyManagement tag.
<dependencyManagement>
<dependencies>
<dependency>
<grpId></grpId>
<artifactiID></>
<version/>
</dependency>
</dependencies>
</dependencyManagement>
Now my POM is only taking mssql version from spring boot parent (Which is obvious as per the tree hierarchy) but i want to use version from BOM POM.
I also don't want to hardcode the mssql version in my pom dependency like
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>It should take from the BOM POM declared in dependency management</version>
So that whenever a new version got updated in my BOM POM, it should reflect directly in my application POM file.
my question can be repetitive or easy to figure out but as i am new to the spring world, Your help will be helpful for sure.
Thanks is advance.
Not possible.
Parent POM versions are stronger than BOM versions.
You need to add the version explicitly in your POM and change it manually when the BOM changes.
To override the version of a dependency you have to find how it was defined in the parent POM. Example in your case, if you look at the Spring Boot parent POM, in the <properties> section, you should see something like:
<mssql.version>Y.XX</mssql.version> <!-- please note that I am not really sure about the property name but I am 100% that the version is defined as a property-->
Then copy-paste that property in the <properties> section of your POM and set the version you want.

Do the projects created by archetype plugins use old versions of dependencies?

I am running
mvn archetype:generate
and then choose maven-archetype-quickstart archetype plugin. Then it creates a maven project with a 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>ocean.earth</groupId>
<artifactId>mytest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mytest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I find the version of JUnit is 3.8.1, surprisingly old, given that the latest version of JUnit is already 5.0.1, and I am using the latest version of Maven 3.5.2.
I was wondering whether it is common that the projects created by archetype plugins use old versions of dependencies?
How shall I make the plugins create projects using newer versions of dependencies?
Thanks.
The short answer to this question ...
Do the projects created by archetype plugins use old versions of dependencies?
... is: the <dependencies/> block in the pom.xml produced by maven-archetype-quickstart is a snapshot of the current versions of those dependencies as they were when the archetype artifact was created.
For background: an archetype is (more or less) an archive which contains some static content along with a mechanism for substituting user supplied values for tokens within that content.
The maven-archetype-quickstart includes some static content (a pom.xml) and supports token substitution allowing you to supply your own values for groupId, artifactId, projectVersion etc.
So, when you create a project from this archetype the static pom.xml is updated with your supplied values and the updated version is the POM for your newly created project.
The pom.xml within the archetype also contains <dependencies/> such as:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
The <dependencies/> block is entirely static and cannot be updated by anything you supply when running the archetype command so the junit dependency will always remain 3.8.1. The choice of 3.8.1 is a result of when the latest version of the maven-archetype-quickstart artifact was created: April, 2010.
So, it's fair to say that the maven-archetype-quickstart artifact is - with respect to the <dependencies/> block - out of date. The other things which the archetype does (creating a pom.xml with your groupId, artifactId etc and creating the standard Maven project structure) remain valid.
In answer to this:
How shall I make the plugins create projects using newer versions of dependencies
You could ...
Update the versions produced by the maven-archetype-quickstart
Create your own archetype with later versions than those provided by maven-archetype-quickstart
Create your own pom.xml without using the archetype

No versions available for org.eclipse.swt.win32.win32:x86_64:jar

While trying to depend on piccolo2d-swt-examples artifact (in m2e), I am getting the following message
VersionRangeResolutionException: No versions available for org.eclipse.swt.win32.win32:x86_64:jar:[3.3.0-v3346,) within
specified range
What is the meaning of this message? Does it mean that there is no library for win32 at all? Or it means that SWT is not under maven control?
UPDATE
My current POM is below.
Currently it has no any explicit dependency on SWT. Being not fluent with Maven, I can't judge if this message means, that Maven feels my SWT version from global settings and reports, that no library written for it, or it just can't find any required SWT libraries in repository.
In first case I can't use SWT version of Piccolo at all (it is not portable, since not written for all platforms) while in second case I can use it, but need to pack SWT for Maven in local repository.
This is the question.
<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>tests</groupId>
<artifactId>Piccolo2D_3_Tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-swt-examples</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
VersionRangeResolutionException: No versions available for
org.eclipse.swt.win32.win32:x86_64:jar:[3.3.0-v3346,) within specified range
According to a maven central query there is no x86_64 artifact for that swt jar file but there is an x86 artifact.
You might want to try forcing your piccolo2d dependency to activate its windows_x86 profile explicity using mvn -Pwindows_x86 or (UPDATE 2) try building with a 32-bit Java JDK.
UPDATE 1: Your problems look similar to piccolo Issue 203: Missing maven profile for Windows x86_64.
I downloaded the the missing artifact from here(http://www.java2s.com/Code/Jar/o/Downloadorgeclipseswtwin32win32x866442jar.htm) and installed the jar using
>mvn install:install-file -Dfile=org.eclipse.swt.win32.win32.x86_64-4.2.jar -DgroupId=org.eclipse.swt.win32.win32 -DartifactId=x86_64 -Dversion=4.2 -Dpackaging=jar
And it started working fine :)
PS: This way you don't need to tamper with the profiles at all.
This is from the Piccolo readme:
To include the Piccolo2D core classes in your project, use a
dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-core</artifactId>
<version>1.3.1</version>
</dependency>
in your pom.xml. To include the Piccolo2D core classes and the
Piccolo2D extras classes in your project, use a dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-extras</artifactId>
<version>1.3.1</version>
</dependency>
in your pom.xml. To include the Piccolo2D core classes and the
Piccolo2D SWT classes in your project, use a dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-swt</artifactId>
<version>1.3.1</version>
</dependency>
Does adding these dependencies change anything?

How to compile a maven project which reference to another local changed maven project?

I have 2 projects, A and B. B is a lib project and A reference to B.
When I add new function to B, it's ok to run mvn install on B, but it failed on mvn install on A due to can not find the symbol from new B.
I'm sure I did install correctly on project B, but why A still failed to compile and install?
This is A's pom.xml:
<dependency>
<groupId>A and B's group</groupId>
<artifactId>B</artifactId>
<scope>provided</scope>
</dependency>
any clue? Thanks
I would suggest to create a multi-module build like the following:
+-- root
+-- pom.xml
+-- module-A
+-- module-B
In the root pom you need to define the modules like this and define the packaging to pom.
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
Furthermore you can define a dependency of module-A to module -B simply by:
<project ..
<parent>
<groupId>project.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-A</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-B</artifactId>
<version>${project.version}</version>
</dependency>
..
</dependencies>
..
</project>
With this setup you can simply build all modules from the root folder just by:
mvn clean package
or you can import that structure into Eclipse (m2e installed?) or any other IDE like IntelliJ or Netbeans.
Try mvn clean install to do a totally fresh build of A, or mvn -U install to force Maven to look for updated snapshots. It sounds like your environment is still using the older JAR. It's hard to tell what your setup is from this description -- sounds like you're correctly installing B to your local repository, but I'm not sure if your IDE might be trying to be 'helpful' as well.
You can include the <version> element in the project's as well as dependency's declaration.
pass a version variable from Maven command line. Eg: ${build.version}
Module A's pom.xml:
<groupId>A & B's Group ID</groupId>
<artifactId>A</artifactId>
<version>${build.version}</version>
<dependencies>
<dependency>
<groupId>A & B's Group ID</groupId>
<artifactId>B</artifactId>
<version>${build.version}</version>
<type>war</type>
<scope>provided</scope>
</dependency>
...
So, whenever Project B is built and installed, the dependency will be available in the local maven repository for that particular version and will be picked by Project A

Mulit-module application maven

I've got multi module maven project, where main project depend on sub-module. Every dependency of sub-module is define by version like this: ${pom.version}. I use maven release plug-in. If I try to prepare release, I've got an error about missing version of sub-module.
Example:
main pom is on version 1.0, I try to release it. Maven build every sub-module to version 1.1, then try to build parent, and then crash. Because it can't find sub-module-1.1.
I don't know how to tell maven to build, and immediate install to local-repo every sub-module witch it build. I use maven2.
My pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.voncuver</groupId>
<artifactId>voncuver</artifactId>
<packaging>pom</packaging>
<version>1.1-SNAPSHOT</version>
<name>multimodule</name>
<modules>
<module>mod1</module>
<module>mod2</module>
</modules>
(...)
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>mod1</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
<dependency>
<artifactId>mod2</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
(...)
You should probably post a bit more of your project structure, but typically a multimodule project looks like this:
project
mod1
mod2
mod3
pom.xml
The main pom.xml would have "pom" packaging type, and have a section in it to build everything else:
<packaging>pom</packaging>
<modules>
<module>mod1</module>
<module>mod2</module>
<module>mod3</module>
</modules>
Then, the surest way to make sure things build properly is to execute:
mvn clean install
Without the "install", it's highly possible that things might not be found in the maven reactor, especially depending on what version of maven you are using (and a few other factors).

Categories

Resources