Usage of maven ${basedir} in multi-module setup - maven

I am using a local repository as described in Maven: add a dependency to a jar by relative path.
The repository-url is defined in the topmost pom.xml as
<url>file:${basedir}/../3rdParty/maven-repository</url>
Also, the topmost pom.xml defines 2 modules
<modules>
<module>sub1</module>
<module>sub2</module>
</modules>
The problem is, that if a module (say sub1) defines a dependency that should be downloaded from the repository, and maven is called from the topmost directory, the ${basedir} is not set to this directory, but to sub1, resulting in a wrong repository-URL.
So, say the project with the topmost pom.xml resides in
/Development/myproject/pom.xml
And the repository is in
/Development/3rdParty/maven-repository
Then the repository URL should be set to
/Development/myproject/../3rdParty/maven-repository
but it turns out it is set to
/Development/myproject/sub1/../3rdParty/maven-repository
which of course does not exist.
Any idea why that is the case?

Although it is annoying in your case, this is well-known and intentional. A maven project should know about its execution directory only, no matter in what context it is executed.
I asked almost the same question: Maven variable for reactor root earlier, and the only answer that made sense was to use ${user.dir}, although it's hacky and will not work if you build from a module directory.
(There is also this very verbose solution: Maven2 property that indicates the parent directory)

You can determine the root directory of a module with this nice plugin:
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.3.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>directory-of</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>maven-parent.basedir</property>
<project>
<groupId>my.group</groupId>
<artifactId>maven-parent</artifactId>
</project>
</configuration>
</execution>
</executions>
</plugin>
Use the directory like this: ${maven-parent.basedir}

How about having multiple repos?
<repositories>
<repository>
<id>ibm-jars-bundle-lv0</id>
<url>file://${basedir}/ibm-jars-bundle/repo</url>
</repository>
<repository>
<id>ibm-jars-bundle-lv1</id>
<url>file://${basedir}/../ibm-jars-bundle/repo</url>
</repository>
<repository>
<id>ibm-jars-bundle-lv2</id>
<url>file://${basedir}/../../ibm-jars-bundle/repo</url>
</repository>
</repositories>

Related

Request a custom ant task from a private repository in a maven-antrun-plugin

I am trying to execute a custom ant task, defined in a jar which is not under Maven Central, and thus I created a private repository for this.
The repository is defined with something like this, which hosts also other "private" jars, which are prefectly discovered:
<repositories>
<repository>
<id>repository.com</id>
<name>repository.com</name>
<url>http://repository.com/maven/</url>
</repository>
</repositories>
Now the question:
Although in the documentation states to define the dependency inside the <plugin> section only, I have found that I need to also define the dependency also inside the generic <dependencies> of the pom file.
So practically something like this, which duplicates the dependency descriptions:
<dependencies>
<dependency>
<groupId>org.bitbucket.infinitekind</groupId>
<artifactId>appbundler</artifactId>
<version>1.0ea</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>local-install</id>
<phase>install</phase>
<configuration>
<target>
<taskdef name="appbundler" onerror="fail" classpathref="maven.plugin.classpath" classname="com.oracle.appbundler.AppBundlerTask"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.bitbucket.infinitekind</groupId>
<artifactId>appbundler</artifactId>
<version>1.0ea</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Is this the expected behaviour or I am missing something?
EDIT
It seems even then, the dependency is not fulfilled.
Although I can clearly see that (due to the generic depenedency) the jar is downloaded and placed in ~/.m2 repository, ant task still doesn't understand it:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (local-install) on project cmmanager: Execution local-install of goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run failed: Plugin org.apache.maven.plugins:maven-antrun-plugin:1.8 or one of its dependencies could not be resolved: Failure to find org.bitbucket.infinitekind:appbundler:jar:1.0ea in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
Well, after some more digging, I found the answer. The problem is that plugin dependencies are resolved not through the repositories section, but through the pluginRepositories sections.
So this will fix the problem:
<pluginRepositories>
<pluginRepository>
<id>repository.com</id>
<url>http://repository.com/maven/</url>
</pluginRepository>
</pluginRepositories>

java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication

This is a follow up question to this question:
java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication Maven
I have developed a web service using Eclipse on Windows. I need to run it on a Solaris station and there I get the ClassNotFoundException.
This is my pom.xml: (that maven shade plugin is something I've read about that can create an uber jar with all the dependecies).
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.group.id.Launcher1</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
And this is the exception I get when trying to run the jar on Solaris:
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
The project got a "Maven Dependencies" folder with many jar files of the Spring framwork.
I did not really understand the answers to that question. The main answer suggested creating a consolidated jar as shown here: How can I create an executable JAR with dependencies using Maven?
But i did not fully understand how to create that conolidated jar. Do I need to add the lines in the answer to the pom file? or somewhere else? and running the mvn clean compile assembly:single should be done in windows in the command line? and if so on which path? won't just exporting to jar suffice?
If someone could make a list of operations of what to do, that'll be great.
What no one ever said in the answers to other questions is that you need to use maven to create the jar and not using Eclipse's export to jar option. What you need to do is:
1) download maven from https://maven.apache.org/download.cgi
2) The maven dir contains a 'bin' folder. Add this folder to your "path" enviornment variable (on Windows 8 right click "This PC" -> properties -> Advanced System Settings -> Environment Variables -> in System Variables find "Path" -> double click it and add it by adding the bin folder path to that variable the same way other paths are located there.
3) open CMD
4) navigate to your project's folder
5) type mvn package
The jar file is created inside the "target" folder.
Good luck
The spring-boot-maven-plugin already creates a runnable JAR with all dependencies in it when you run mvn package (this is one of the main purposes of Spring Boot). So remove the maven-shade-plugin, because this is not necessary (in fact, i think it is the real problem because it will interfere with Spring Boot).
what worked for me:
cd to THE_PROJECT_FOLDER via the cmd or git bash
mvn clean
maven install
maven package
go to the project using your IDE and go to the class containing the main method
Click on the #SpringBootApplication and see to the left an icon like a lamp
Click on it and select download with maven
Restart the server, everything is OK !!!!
Adding the following plugin in pom.xml solved the issue for me:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

how to add external jar to maven webapp project

I have a Spring roo project (basically a maven project). I want to add dropbox sdk to the project, problem is it's not in maven. I added the following files
<dependency>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/dropbox-java-sdk-1.3.1.jar</systemPath>
</dependency>
It solved the compile error, but when i run the project, in Spring Tool Suite, the jar files are not added to war lib folder. How do I make maven add my external jar files to my the war lib folder?
I don't want to install the jar in maven since, I have to install it in all the machines that uses the project
I finally found a neat solution, which is a lot easier to implement. You add an in-project repository inside the java project and link to it in the pom.
You add an in-project repository in maven like this:
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
Then create a folder structure in the root folder of your project that looks something like this
/groupId/artifactId/version/artifactId-version.jar
and add the dependency as you would normally do.
This approach has the least amount of code and work required, and if that library ever gets add into a maven repository you can always remove your in-project repository.
There is a much easier solution, which is set webResource in the plugin. By the solution, you can add any files of your local disk to the war! A sample is as below,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>api</warName>
<webResources>
<resource>
<directory>libs/</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
The best way to resolve this issue is to add these local jar files to WEB-INF/lib folder. You will find all these jars packaged in your final war file then.
I don't recommend this approach, but you could add some POM configuration to install the 3rd-party dependency in a separate profile:
<profiles>
<profile>
<id>install-dependencies</id>
<build>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install-dropbox-sdk</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
<file>src/main/lib/dropbox-java-sdk-1.3.1.jar</file>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
There are two profiles here: install-dependencies and build. The first installs the dropbox-sdk dependency into your Maven repository and needs to be run once on every machine as follows:
mvn -Pinstall-dependencies validate
The second is enabled by default, and adds the Dropbox SDK as a dependency.
To be honest though, this isn't much better than running
mvn install:install-file -Dfile=src/main/lib/dropbox-java-sdk-1.3.1.jar -DgroupId=com.dropbox -DartifactId=dropbox-sdk -Dversion=1.3.1 -Dpackaging=jar
on every machine.
The other downside of this approach is that you'll have to add all dependencies of the dropbox-sdk to your build as well- whereas if it is done properly by adding the JAR and a POM to a repository server, then Maven will calculate the transitive dependencies properly.
I recommend creating a "third party" repository in a Maven repository server such as Nexus or Artifactory, and uploading the jar to there. Even though that means putting the jar into Maven, at least with a repository server it is available to anyone who will be building your application.
change the lib path to :
src/main/webapp/WEB-INF/lib
in pom.xml:
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/xxxx.jar</systemPath>
The steps described in this site are pretty simple, and they work well enough:
https://mythinkpond.com/2010/10/02/adding-custom-jars-under-web-inflib-in-a-maven-project/
Create a “lib” folder under your project like this: “\src\main\webapp\WEB-INF\lib”
Copy needed “jars” etc that you want included inside your WAR bundle folder.
Invoke your maven build as you normally do. I use “mvn install”, which creates builds the war file.
I know I am really late but I was wondering on why you would not put in the jar in the local repo in the .m2 file and add a reference to the pom from there ?

mvn deploy:file to different repositories for snapshot and release version

Is it possible to in some way tell the maven deploy:file goal to deploy to two independent artifactories based on whether the version of the project is a snapshot / release?
I'm hoping there might be a property which indicates the fact the version has -SNAPSHOT prepended, or perhaps the default artifactory to deploy to (which has been worked out based on the version number already).
I thought about using two different profiles and working out if its a snapshot in ant by parsing the pom.xml file, but I'd rather a cleaner solution if possible.
Currently, my deploy plugin looks as follows, but this just deploys to the release artifactory regardless of the version;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>deploy-zip-created-by-ant-to-artifactory</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
<url>${project.distributionManagement.repository.url}</url>
<file>${project.basedir}/Build/deploy/MyArtifact.zip</file>
<pomFile>${project.basedir}/MyArtifact-pom.xml</pomFile>
</configuration>
</execution>
</executions>
</plugin>
Many Thanks
If you defined your repositories within your settings.xml you can use the
mvn deploy:deploy-file -DrepositoryId=releases -DartifactId=... -Durl=
Over here, I used the GMaven plugin to choose the repository from the distributionManagement section of the POM and store it in a property.
The deploy plugin can then use that property.
Maybe you want to use the build-helper-maven-plugin to deploy an additional artifact
This is presumably the Maven way:
<distributionManagement>
<repository>
<id>release</id>
<url>http://my-releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://my-snapshots</url>
</snapshotRepository>
</distributionManagement>
When doing a deploy of a snapshot version, it'll go the snapshots repository. For a non-snapshot release the regular repository will be used.
Just run deploy and it'll work. :-)

In Maven how do I copy files using the wagon plugin?

Summary: How do I copy some generated files into a webserver (eg IIS or Apache) directory using Maven?
Details:
I have a working application that builds in Maven. I've managed to get it building using the webstart-maven-plugin which produces all the needed files (.jar and .jnlp) in a directory target/jnlp. It also creates a zip file with them all in at target/foo-1.0.zip.
At the moment the webstart plugin does not have a deploy goal - the request for it has ended up on the FAQ (question 3). It may be implemented in future, but the suggestion for the time being is to use wagon-maven-plugin.
I've never used Wagon. To start with I'd like to just copy the files to a local directory served up by a webserver. Later I'd like to copy them remotely, probably using ftp. Can someone give an example to what I need to add to the pom.xml to get the local copy working (and hopefully an ftp example too?). I can't find it in the documentation. From reading I think I might also need the Wagon Maven File Provider but as this seems to have almost no documentation I'm not sure.
Wagon providers are only there to provide additional network protocol supports (such as FTP).
If you want to copy file to a webserver (local or distant) you can use Maven upload plugin :
...
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-upload-plugin</artifactId>
</plugin>
...
In parent pom :
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-upload-plugin</artifactId>
<version>1.1</version>
<configuration>
<resourceSrc>
${project.build.directory}/${project.build.finalName}.${project.packaging}
</resourceSrc>
<resourceDest>${jboss.deployDir}</resourceDest>
<serverId>${jboss.host}</serverId>
<url>${jboss.deployUrl}</url>
</configuration>
</plugin>
And to configure parameters in a smart way, I use maven profiles (in parent pom) :
<profiles>
<!-- local deployment -->
<profile>
<id>developpement</id>
<properties>
<jboss.host>localhost</jboss.host>
<jboss.deployDir>appli/jboss-4.0.4.GA/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>file://C:/</jboss.deployUrl>
</properties>
</profile>
<!-- distant deployment -->
<profile>
<id>validation</id>
<properties>
<jboss.host>ENV_val</jboss.host>
<jboss.deployDir>/home/envval/jboss/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>scp://PROJECT_LAN_HOST</jboss.deployUrl>
</properties>
</profile>
</profiles>
I've created an "ant launcher", to use it by clicking under Eclipse ant view :
<target name="copy war to JBoss local" description="Copy war to local JBoss">
<maven goal="upload:upload" options="-Pdeveloppement" />
</target>
But you can simply run it on a command line :
mvn upload:upload -Pdeveloppement
EDIT : By the way, for distant deployment, you may need a login password for scp to work. You have to add them to you Maven settings.xml file :
<settings>
...
<servers>
<server>
<id>ENV_val</id>
<username>login</username>
<password>password</password>
</server>
</servers>
...
</settings>
EDIT: You'll need to add the Atlassian repository:
<pluginRepositories>
<pluginRepository>
<id>Atlassian</id>
<url>https://maven.atlassian.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
EDIT: depending upong the remote protocol you'll have to add wagon extensions, see Uploading a directory using sftp with Maven
In the end I didn't use the Maven upload plugin - it seemed a bit limited and not part of the main maven distribution. I used the maven wagon plugin as suggested. Here is the simplest possible pom that I could make that worked. Hopefully others will find it useful, as I couldn't find anything similar easily.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<configuration>
<fromDir>${project.build.directory}/jnlp</fromDir>
<includes>*</includes>
<url>file://c:/inetpub/wwwroot</url>
<toDir>jnlp</toDir>
</configuration>
</plugin>
For remote distributions, you just change the URL type, and possibly add wagon extensions as necessary.

Resources