latest version of a dependency in maven archetype [duplicate] - maven

Are there any preexisting Maven plugins or commands to update the dependencies in the POM?
Example: (if this was in my POM)
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
Is there a command or plugin I can run to get it to update the dependency to:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>

Try the maven-versions-plugin, in particular, the versions:use-latest-versions goal.

I prefer using mvn versions:display-dependency-updates; this generates a report of which dependencies can be upgraded, but lets you modify the POMs yourself. There's also a display-plugin-updates command for plugins.

you can use dependencyManagement in your parent pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</dependencyManagement>
this way, you need to change the version only once in the parent POM

Personally, I think there should be an additional parameter in maven that would allow you to add to the pom.xml.
See post at http://maven.40175.n5.nabble.com/Is-there-any-maven-plugin-to-add-dependency-to-existing-pom-xml-td2839092.html#a5772853
Here, you can add the following to your pom.xml file:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
...
Then backup your pom.xml file via version set command:
mvn versions:set -DnewVersion=9.9.9
Run latest versions:
mvn versions:use-latest-versions
and diff the pom.xml files, pom.xml and pom.xml.versionsBackup

No there is isn't. And be happy there is not. How would such a tool know how to upgrade your dependencies?
With breakages possibly happening between minor versions, it would be a disaster waiting to happen.
But you can always write your own Mojo for that.
get latest version of dependency from Maven repository
compare with version from pom.xml
rewrite pom.xml
run mvn test
?
Profit!

I had the same kind of problem and finally solved it by writing a bash script.
GitHub repository - Update POM Shell
This is a shell script that allows you to update a dependency on different modules directly from the command line.
It is particularly useful when you need to update one or more dependencies on different modules at once.

Related

Maven Wrapper install plugins locally

I'm using Maven Wrapper for the first time and I am trying to run a command './mvnw -B openl:test' locally but I keep getting the error that no plugin found for prefix 'openl' in the current project and in the plugins groups. I looked in the .m2 directory and I do see the openl maven plugin there so I not sure why it's not working. I installed the plugin running './mvnw clean install' after added the dependency to the pom.xml file.
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>openl-maven-plugin</artifactId>
<version>5.21.9</version>
<scope>test</scope>
</dependency>
Am I missing something?
If you want to use plugins with maven you need to declare it under "plugin" section in pom.xml. In your case it would look like this:
<build>
[...]
<plugins>
[...]
<plugin>
<groupId>org.openl.rules</groupId>
<artifactId>openl-maven-plugin</artifactId>
<version>5.21.9</version>
<extensions>true</extensions>
</plugin>
</plugins>
[...]
</build>

groovy-maven-plugin: How to add a dependency for executing groovy scripts?

I want to execute a small groovy script via the groovy-maven-plugin(or gmavenplus) during build, I need a third-party dependency, but the project itself doesn't need this dependency, how to add this dependency only for executing groovy scriptes?
Add the needed library as a plugin dependency.
<plugin>
<artifactId>groovy-maven-plugin</artifactId>
...
<dependencies>
<dependency>
....
</dependency>
</dependencies>
</plugin>

Add external library .jar to Spring boot .jar internal /lib

I have an external .jar that cannot be imported from public repositories using pom.xml, it's sqljdbc41.jar.
I can run the project locally from my IDE, and everything will work. I referenced the library after downloading it like so:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
When I run mvn clean package to create my .jar file and try to run the created .jar, a mistake will pop up, which mentions the SQL Server references are not valid. I then extracted my .jar file and true enough, everything that is referenced in the pom.xml file properly gets downloaded and added, however, my SQL Server does not.
I can, in a very hacky way* just manually add the sqljdbc41.jar to my /lib folder after it's been compiled as a .jar, and it'll work, however that seems highly unoptimal. What would be a better approach?
*Opening the .jar file with Winrar, going to the /lib folder, manually selecting my sqljdbc41.jar file, then make sure to select the No Compression option bottom left where Winrar gives you compression options, in case you find this by Google and no one answered.
you can set 'includeSystemScope' to true.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
You could install the sqljdbc41.jar in your local repository :
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
And then declare the dependency as a standard dependency :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
If you use a remote artifact repository (nexus, archiva...) you also need to deploy the artifact on this repository. You can find more here : https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Another way, you can put it into the resources folder, such as resources/lib/xxx.jar, then config the pom.xml like this:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
</dependency>
In Spring Boot: I also faced similar issue and below code helped me.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
It works for me:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
In my case, the fault was providing a version number without "dot" in tag:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
This one works:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
When Spring-Boot projects are used with maven or gradle plugins they packaged the applicaiton by default as executable jars.
These executable jars cannot be used as dependency in any another Spring-Boot project because the executable jar add classes in BOOT-INF/classes folder. This means that they cannot be found when the executable jar is used as a dependency because the dependency jar will also have the same class path structure as shown below.
If we want to use project-A as a maven dependency in project-B then we must have two artifacts. To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
To configure a classifier of exec in Maven, you can use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
So the MAJIC WORD here is <classifier>exec</classifier> this will create a jar structure as below and then it could easily be conusmed by spring-boot project as maven dependency jar on class path.
The above plugin need to be add in project-A pom that is going to be used as dependency in project-B. Same is explained in spring documentation section 16.5. as well.
In order to work through the local repository, the target .jar file that we will work with must be in the s2 folder. Several methods can be used for this:
The file can be taken manually and put in the relevant place (not
preferred). The same process can be done by installing it via the
console.
Relevant Remote URL is written in the .pom file dependencies and
automatically places it in the s2 folder when Intellij is refreshed
(validate) in the IDE used.
The same process can be done by addressing the .pom file dependencies via the centeral repository.
Attention: ComponentScan should not be forgotten for the related jar work on SpringBot.

Override dependencies in a maven plugin (one plugin, different modules and different dependencies)

I have a java project which has more modules. In some of the modules I use a plugin.
In that plugin I nee to override one from its dependencies. So far so good. Solved with adding of the desired dependency in the plugin definition.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xdoclet-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xjavadoc</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
</dependencies>
...
But in one of the modules I need to override that dependency with some other version. I tried to solve that with the same way as overriding.
If I compile only that submodule alone, the correct dependency version is used, but if I compile whole project it does not work, because it uses the dependency from other modules for the plugin and not the one specified in the modules pom.
Any idea how to solve my problem?
Best regards, Filip
You can create property for your plugin version, e.g xdoclet.version and use it to override parent version.
<properites>
<xdoclet.version>1.5-SNAPSHOT</xdoclet.version>
</properties>
...
<plugin>enter code here
<groupId>org.codehaus.mojo</groupId>
<artifactId>xdoclet-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xjavadoc</artifactId>
<version>${xdoclet.version}</version>
</dependency>
</dependencies>
...

How to properly link two Maven projects?

I have two projects:
Project-Core
Project-Source
Project-Core POM.xml:
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<packaging>jar</packaging>
<version>2.1</version>
Project-Source POM.xml:
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<version>2.1</version>
<type>pom</type> (have tried leaving it out)
</dependency>
</dependencies>
I've done mvn clean install on Project-core, which installed the artifact in the local maven repository.
I am able to CD to Project-source and use mvn clean install (this installs Project-Source in the local maven repo as well), but I'm having trouble with NetBeans not finding the classes I need (from Project-Core) inside Project-Source.
What's a proper way of linking multiple projects? Since Project-Core produces a jar and that jar is installed in the local repository, it looks logical to only have to list that jar as a dependency on my Project-Source project. Is anything else needed?
You specified that the dependency "project-core" is of type "pom", but from the declaration it has packaging "jar" ?
Try:
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
Edit:
I've created a simple test project which worked just fine to use in Netbeand 7.0.1. Take a look and see if it gives you any hints.Code snippet

Resources