Use Maven to customize and build another Maven project - java

What I want to achieve is the following:
Checkout external project from svn
Replace spring.xml with my customized version
Change version of a dependency
Build
Package
Are there standard maven plugins to handle all these goals?

For svn operations, you can use: org.tmatesoft.svnkit:svnkit
For replacing spring.xml with a customized version: maven-assembly-plugin, maven-resources-plugin would both work, depending on what exactly you need to do (I'm assuming there's more work here).
For building and packaging, you can use the maven-invoker-plugin. That plugin basically lets you point at a project and run "mvn clean package" or whatever other maven commands you want such as invoking a plugin. It's pretty much just a way to run maven from maven.

Related

Ant use Maven's library

I wrote a project which use maven. It's good for me.
But, some other people don't have maven, so I am trying to write an ant build.xml for them.
My question is:
Inside "javac" -> "classpath" tag, how could ant get the libs I used in the project from maven? So that could compile & pack all the 3rd-party libs into release via ant.
Is this possible or there are better solution for maven & ant exists in same project.
Ant combined with Ivy is your answer. Ant can use a Maven repository to pull in jars, and can even be made to output a pom.xml, so the jar can be deployed back to a Maven repository.
I have an ivy.dir project on Github that I use to help integrate Ivy into already existing Ant tasks -- especially if they use Subversion as a version control system. You can create a ivy.dir subproject, and make that an external on the Ant project.
Have a look at ivy. Use it to download your dependencies for ant from a maven repository.

Running a Maven project with many dependencies

I'm hacking on a Maven-based project with a lot of dependencies; the project is normally meant to be developed in Eclipse but I need to work on it from the command line.
How to build+execute the project in a sane way? Something like mvn run, but of course Maven is not meant for running Java projects (for some reason).
The problem is specifying all the dependencies on java's commandline, I don't even know how to autogenerate that. I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me.
However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please?
Note: One possibility is running it in Eclipse once and capturing the java commandline. However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore.
Have a look at the maven exec plugin
mvn exec:java -Dexec.mainClass="com.example.Main"
if you do this frequently, you can of course configure it via plugin configuration.
Regarding finding out project dependencies - you can use maven dependency plugin
http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html
If you want to put them into file it'd be smth like
mvn dependency:list > dependencies.txt
See this question: How can I create an executable JAR with dependencies using Maven?. You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest.
I see three solution to this:
onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor
With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside.
Spring Boot Maven Plugin - but this is dedicated to Spring projects
Maven Assembly Plugin with custom descriptor. This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. After this you can simply execute your program or zip your jar with lib folder as distribution.
After this is possible to run your program on your computer or any other with one command:
java -jar myjar.jar

How do I create a JAR with m2e (m2eclipse)?

I'm new to Maven and m2e. It frustrates me that I have to ask this question, but the sparse m2e documentation and Google are failing me.
How do get m2e to build a JAR? I understand that this should happen during the maven package phase, but m2e doesn't seem to do this as part of the build process and I can't find a way to explicitly execute the package phase in Eclipse (nor any other phases that aren't part of the default build).
Thanks.
As long as you have your POM.xml file with the following parameters:
<modelVersion>[a model number eg 4.0.0]</modelVersion>
<groupId>[a group id eg com.myapp]</groupId>
<artifactId>[a unique artifact id within your packages eg myapp]</artifactId>
<version>[the version number eg 1.0-SNAPSHOT]</version>
<packaging>jar</packaging>
<name>[the name eg myapp]</name>
then you just need to run maven build with the goals clean install to create a jar file from your project. You can run maven build by right clinking on the project and going to run > maven build ...
The jar will be created in [project dir]/target
Although "Run As maven install" would do the trick, it can be good
to know that m2e will perform the equivalent of the package phase when doing "Export... Jar/War/EAR file". It seems to understand the plugin configurations too, at least a little bit, and at least for EARs...
As it will resolve artifacts using projects and the m2 repository,
it will also work for "unrelated" modules, as the dependency that resolves to a project is good enough for eclipse to package.
(That is, you don't have to install the unrelated dependency separately, it will be built automatically from the eclipse project.)
I'm not sure I would deploy anything it builds though :-)

Eclipse: Dependency Management

What are some methods of utilising Eclipse for Dependency Management?
I really like the The Maven Integration for Eclipse (m2eclipse, Eclipse m2e). I use it purely for the dependency management feature. It's great not having to go out and download a bunch of new jars new each time I set up a project.
A simpler way to go is the Maven Eclipse plugin (as opposed to a Maven plugin for Eclipse). It's simply a maven plugin that generates the .project and .classpath file based on the contents of the pom, you just run mvn eclipse:eclipse and you're done. It uses a classpath variable in Eclipse to locate the local maven repo.
I personally prefer this approach most of the time because you have more control over when the maven plugin updates are done. It's also one less Eclipse plugin to deal with. The GUI features of the m2eclipse plugin in the latest version is pretty nice, though.
There's also an alternative to the m2eclipse plugin called Q4E, now called Eclipse IAM.
Another option is ivy. Ivy has eclipse integration as well.
A comparison of maven and ivy can be found here:
http://ant.apache.org/ivy/m2comparison.html

How do you refresh maven dependencies from eclipse?

We recently started using maven for dependency management. Our team uses eclipse as it's IDE. Is there an easy way to get eclipse to refresh the maven dependencies without running mvn eclipse:eclipse?
The dependencies are up to date in the local maven repository, but eclipse doesn't pick up the changes until we use the eclipse:eclipse command. This regenerates a lot of eclipse configuration files.
Have you tried using the m2eclipse plugin? I use it with eclipse and it maintains the eclipse .classpath when I add dependencies. It'll also check for updated dependencies.
You generate the special eclipse files with mvn eclipse:eclipse, but once you've done that, you should let a plugin handle the dependencies while inside eclipse.
That's how we do it at my work place, and it generally works well.

Categories

Resources