How to configure Eclipse to automatically resolve and build multiple maven modules? - java

Here is my situation:
I'm trying to migrate from Ant to Maven
My project has 3 artifacts: shared api (jar), web app (war), desktop swing app (jar). Latter 2 depends on shared api.
At this moment I'm trying to make web app part work. So I've created 4 poms: eftracker (root pom), eftracker-parent, eftracker-shared, eftracker-web.
If I run mvn package on eftracker all works just perfect -- I have eftracker-shared.jar and eftracker-web.war created as expected
I added tomcat7-maven-pluginto run web app with maven goal tomcat7:run to test changes made during development
I also added eftracker-shared as a project to eftracker-web build path.
My goal:
Now I want to work comfortably in Eclipse, meaning I want to change files, hit Run and in couple seconds be able to test my changes.
During development I will change both: shared and web projects.
My problem:
If I never run mvn install than an attempt to invoke tomcat7:run will lead to error: Failed to execute goal on project eftracker-web: Could not resolve dependencies for project com.skarpushin:eftracker-web:war:1.503.0: Could not find artifact com.skarpushin:eftracker-shared:jar:1.503.0 in central (https://repo.maven.apache.org/maven2)
It appears I have to mvn clean install shared project (or even on root module) each time I change it before I can execute tomcat7:run on web app and see recent changes.
Question is:
Is it possible to make this process automatic?
...OR maybe there is other way how to minimize "maven overhead" during development?
eftracker.pom
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker</artifactId>
<version>1.503.0</version>
<packaging>pom</packaging>
<name>eftracker</name>
<modules>
<module>eftracker-parent</module>
<module>eftracker-shared</module>
<module>eftracker-web</module>
</modules>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
</project>
eftracker-parent/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.skarpushin</groupId>
<version>1.503.0</version>
<artifactId>eftracker-parent</artifactId>
<packaging>pom</packaging>
<name>eftracker-parent</name>
<!-- ...some common properties, dependencies, build plugins... -->
</project>
eftracker-web/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker-parent</artifactId>
<version>1.503.0</version>
<relativePath>../eftracker-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eftracker-web</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>ROOT##${project.version}</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker-shared</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ...other deps -->
</dependencies>
</project>

Try to use M2Eclipse
https://www.eclipse.org/m2e/
M2Eclipse provides tight integration for Apache Maven into the IDE
with the following features:
Launching Maven builds from within Eclipse
Dependency management for Eclipse build path based on Maven's pom.xml
Resolving Maven dependencies from the Eclipse workspace without installing to local Maven repository
Automatic downloading of the required dependencies from the remote Maven repositories
Wizards for creating new Maven projects, pom.xml and to enable Maven support on plain Java project
Quick search for dependencies in Maven remote repositories

So it appears there are 2 things needs to be done:
run mvn compile on parent project in that way all classes will appear in ../parent/target/classes folder. Note that they'll be automatically updated by Eclipse if you change source code
edit Eclipse run configuration and put this checkbox "Resolve Workspace artifacts"
Now I was able to run project as Maven build... with goal tomcat7:run and it worked without the need of parent project to be installed

Related

Import Maven local .jar as a lib

I made a simple Maven project for my class. According to the teachers tutorial we cannot upload it to our school repo due to some server issues, so we have to store it locally using altDeploymentRepository. I have the following pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.dpp</groupId>
<artifactId>simple_lib</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.basedir}/../${project.name}-mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
</plugins>
</build>
</project>
So in the directory with my Maven project I have two directories:
sample_lib
sample_lib-mvn-repo
In the second one, deep down in : sample_lib-mvn-repo\com\dpp\sample_lib\1.0-SNAPSHOT I have a .jar file which I want to import (but not using just .jar file like passing the path to it - I need to do this "Maven way", import it as Maven lib). Can I do it if the file is not stored on any remote repository, but on my hard drive?
Running simply mvn install will install the file in your local repository. The local repository, by default, is in your home directory, under .m2\repository.
Using your pom above, after running mvn install, you would have jar (and some other files) in .m2\repository\com\dpp\sample_lib\1.0-SNAPSHOT.
To import this subsequently in another project, you would create a dependency in that project's pom like:
<dependency>
<groupId>com.dpp</groupId>
<artifactId>simple_lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
This all takes place only on your machine, and will not use any remote repository.
Now you have a local simulation of a repository.
You can import it using the repository tag as described in https://maven.apache.org/pom.html#Repositories. To specify a file make it a file url like file:
Yes, you can add maven repository and point it to a local directory:
<repository>
<id>local</id>
<name>local</name>
<url>file:${user.dir}/sample_lib-mvn-repo</url>
</repository>
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
Given that your jar file is here sample_lib-mvn-repo\com\dpp\sample_lib\1.0-SNAPSHOT.jar, you then can add it as a dependency:
<dependency>
<groupId>com.dpp</groupId>
<artifactId>sample_lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
It's not exactly what you're asking. But a quick and dirty solution is to make a POM project (no source code).
Inside the POM project you have the main and external projects.
You can simply make a dependency on the other project.

How to add a maven project as a dependency to another one

I've just started a java project, in which I'd like to use the classes of another project.
My pom.xml looks like this so far:
<?xml version="1.0" encoding="UTF-8"?>
<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>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
What would be a good way to add that other project as a dependency in mine? Should I download all of its compiled jar files and add them one by one in my pom.xml as dependencies? Or is there a better option?
I was thinking of downloading all the jars, putting them into a directory (e.g. lib) and somehow referencing that entire directory in the pom.xml, so if there's a new version of the project mine depends on, I only have to change the contents of that lib folder for the new jars, and don't have to edit the pom.xml. Is it an option? If so, how to do that?
Or most importantly, what is the proper way you suggest doing it?
If you want to use the latest version of this project, I suggest you build it yourself. Because it seems they are releasing to Sourceforge and maintaining the code actively.
Each time you want to upgrade the version, you have to get the latest source code (via git) and use mvn install command on this projects root pom.xml to install it to your local maven repo. This project is configured as multi module maven project, using install on the root pom.xml will install all the sub modules.
On your projects pom.xml you can use mvn versions:use-latest-releases to update all your dependencies to the newest version. This command will automatically upgrade dependency versions for you.
To add a project as dependency follow Marvins link.

installing a jar with maven-install-plugin to local repo creates pom without dependencies [duplicate]

I have a maven plugin, which I have not uploaded to the central repository, but which I want to use in my projects.
What works
I can install the maven plugin like this:
git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn install
Then I can use the plugin like this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.wintercloud</groupId>
<artifactId>sample</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>renderjinja</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>out.txt</outputFile>
<templateFile>templ.jinja</templateFile>
<varFile>vars.yaml</varFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here are the other files relevant to the compile:
templ.jinja:
{{ Name }}
vars.yaml:
Name: MyWonderfullName
This works:
> mvn compile
> cat out.txt
MyName
Nice!
What does not work
Now I am trying to give the plugin as a jar to my colleagues so that they can simple install the jar. The Idea is to do it like this:
git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn package
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar
When I now do (in the sample project dir)
mvn compile
I get this error:
[ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml
How can I install the jar so that I can use it as a plugin?
It looks to me as if dependencies are missing. Why?
You just hit MINSTALL-110, which is going to be fixed in the next 3.0.0 release of the Maven Install Plugin. The core issue here is that you're installing manually a JAR file with the file parameter, the plugin detects that there is a POM inside, but only keeps the coordinate information (group id, artifact id, packaging and version), not the whole POM. As such, when the POM is installed, the dependencies aren't kept.
Indeed, if you take a look at the installed POM, you will see
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
<description>POM was created from install:install-file</description>
</project>
which shows that the dependencies weren't retained. And if you take a look at the Maven logs in debug mode when running the install-file command, it will show
[DEBUG] Using META-INF/maven/de.wintercloud/jinja-maven-plugin/pom.xml for groupId, artifactId, packaging and version
A work-around waiting for version 3.0.0 is to specify the path to the POM file to install, along with the main artifact, by specifying the pomFile parameter. Run the following instead:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar -DpomFile=pom.xml
Then the full POM will be installed, not a generic stub.
With this change, the error
[ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml
will not happen anymore. Maven will correctly download the dependencies and use them for the plugin.

How to compile classpath resources on maven package?

I included a project as dependency in another project. On maven package the included dependency is not compiled as jar, but an empty folder is created.
projects:
main-test (packaging: jar)
main-webservice (packaging: war)
the main-webservice project includes the jar with pom.
main-test pom.xml:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>de.main</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
</project>
main-webservice 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>de.main</groupId>
<artifactId>ws</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>de.main</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build><plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins></build>
</project>
The dependencies of the test project are resolved inside webservice project, so the pom seems to be correct.
Anyhow, if I run "mvn package", the resources of main-webservices compile to target, but the libs folder contains another folder named main-test-1.0.jar. BUT it's a folder, not a packed jar.
What could be wrong here? How can I get the project to be packaged as jar, not as folder?
included dependency is not compiled as jar
Maven never compiles dependencies. It just takes them as they are and either puts them on the classpath or copies them into the WAR. But it never changes the artifact.
That also means the dependency must be in your local repo (somewhere below ~/.m2/repository/) or Maven will be unhappy (= fail with an error).
Note: Maven doesn't have a big memory. It can always only keep a single module in his tiny brain. In Eclipse, you can add another project as it is to the build path but Maven can't do that. For Maven to work properly, you must install all the dependencies in your local repo.
Idea:
Check if the jar exists in your repository
Check if the jar project in eclipse is not referenced in the war project in the Java Build Path
Do it without eclipse mvn plugin:
mvn eclipse:clean (for both projects)
mvn eclipse:eclipse (for both projects)
mvn clean install (for both projects)

Maven Release Patterns

I'm new to maven, and trying to understand how to release my project. I have the following project setup in svn:
trunk
|-deployer
| |-pom.xml
|-webapp
| |-pom.xml
|-utils
|-pom.xml
While developing webapp, I always want to develop against the latest snapshot version of utils, so I declare the dependency on utils in webapp/pom.xml via:
com.company
utils
1.0-SNAPSHOT
Webapp itself is also currently versioned at version 1.0-SNAPSHOT. It's pom.xml has the declaration:
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Sample Webapp</name>
So everything is working great, but now I want to release my software. In deployer, I have the following in 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>deployer</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Project Release</name>
<modules>
<module>externals/webapp</module>
</modules>
<properties>
<url.svn>http://<my-server>/<project>/trunk</url.svn>
</properties>
<scm>
<connection>scm:svn:${url.svn}</connection>
</scm>
<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3</version>
<configuration>
<tagBase>
http://<my-server>/<project>/tags
</tagBase>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>Releases</id>
<name>Releases</name>
<url>http://<nexus-server>/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Snapshots</name>
<url>http://<nexus-server>/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
Within the deployer folder itself I have a folder called "externals" which has an svn-externals set to check out my webapp project (I added this because maven complained about not being able to find it), so my modules path should be correct.
Questions
1.) I want to release a version of my web app, but when I run a mvn release:prepare and mvn release:perform in my deployer project, my only tangible output is my trunk tagged in svn and a pom uploaded to my nexus repository. This makes sense in that my deployer artifact packaging is type "pom", but it also doesn't get the job done of getting me a war of my webapp (I should note here that if a do a release in the webapp project by itself though, that I will get the war). I need to release multiple modules, and so I thought I could use maven aggregation from the deployer project to accomplish this, but it doesn't seem to be working.
2.) Is there a better way to achieve what I am trying to do?
Thank you for any insights you can provide.
You should run the maven-release-plugin against each module that you intend to release. I've never seen someone have a special "deployer" module that releases the other modules, this is not how things are commonly done.
Normally to release the webapp module you would run the commands against the webapp module, and to release the utils module you would run the commands against the utils module.
If you have a parent module that ties webapp and util together then I believe you can just run the release commands against that.
As matt b says, simply adding a maven-release-plugin entry at the top pom will probably do what you want. If you want to collect several artifacts into a proper release bundle, you want to create a module for this, but then you want to look at the maven assembly plugin. It can collect various jars (wether from modules or external dependencies) and resources into a directory, zip file or similar.

Categories

Resources