How to run maven commands automatically, not from command? - java

I am working on a project that I need to build a maven pipeline for versioning and release.
I am doing like this:
Change version manually by running mvn versions:set -DnewVersion=2.0.0.
Check the project detail and detect if it is snapshot or release. I have my own custom plugin and a parameter which returns the final version, e.g. 2.0.0-SNAPSHOT. BUT HOW CAN I return this value and feed it to another plugin?
I compile the project and generate the jar with new name, from 2.
And...
I know I can handle every step by a CL command but what I want to know and confused me is that how to embed everything in pom.xml and just run mvn install and nothing else.
Each plugin needs some arguments that should be able to change that. I know I can use the <argument> tag under <configuration>, but some of the plugins do not have the argument tag.
How can I have a solid solution and config everything in my parent POM beforehand?
Also I do have my own plugin but I am not sure if it is possible to embed all things to that and I just run myplugin:Install.

For building a SNAPSHOT, you usually just run mvn clean verify on your project. If you need special plugins, you can configure them in the configuration section following the advice in the plugin description.
For building a release version, you should use a build server (like Jenkins). There, you can construct all the steps much easier than in Maven. If for some reason, you cannot do that, use the maven release plugin.

Related

How to force Eclipse to use maven for automatic build

I am using Eclipse ,Spring,tomcat server,maven.
I need to pass some parameters when i build the project with maven.Everything works fine when i do that using command line or when i right click on POM.xml and run it making run configuration changes in eclipse.
But as soon as i start my tomcat it is not able to find the value i want to pass in the parameter and it gives an exception
: Could not open ServletContext resource [/WEB-INF/classes/properties/application-${spring.profiles.active}.properties]
here ${spring.profiles.active} should be replaced the the argument value.
So eclipse is not using maven with argument to build and deploy on the server automatically.
Insert the required maven plugins (especially maven-eclipse-plugin) in the pom.xml for compiling the code and use the command mvn eclipse:eclipse in the project's base directory. This will do the required stuff to create a maven project in eclipse.
This is a very complicated subject where many things can go wrong. You need to be very explicit of how you are running tomcat. Are you using wtp? WTP would be the panel that says "Servers". Is this spring boot? Do have the m2eclipse wtp plugin installed (its very hidden and not easy to find)?
One easier way to get this right is just use springs version of eclipse.

Using maven to produce production ready output

I have a muti-module maven project, and I created a new module that depends on 3 other modules. (I already have a web app maven module that produces a .war file, now I need this)
This module's output is a .jar, and it has a few resources also which are:
spring context xml file
properties file
Now I want to produce a production ready folder so I can upload it to my server. I am hoping maven can do this for me.
I need the following layout:
myjar.jar
/libs/ (the 3 other maven modules that are dependancies)
/resources
Also, there are some generic dependancies that my parent pom.xml have like slf4j/log4j/ that I also need to package.
It would be cool if I could add a switch to mvn that will produce this like:
mvn clean install production
I plan on running this on my server via the command line.
I think what you are looking for is a Maven Assembly:
https://maven.apache.org/plugins/maven-assembly-plugin/
You can use profiles to disable the generation of the assembly by default (can speed up the development process).
#puce is right in that you may be best to use the Assembly Plugin. What you can't do easily is add another lifecycle 'production' to maven. If you have time you could write a plugin to do this, but you might be better off using a profile called 'production' or 'prod-deploy' to enable the coping into place on the server.
mvn clean install -Pprod-deploy
One thing to remember with maven is that it is very good at building projects in using it's conventions, but it is pretty bad at actually script things to happen out side of the build lifecycle.
I have on several occasions used external scripting tools such as ant/python/bash and groovy to first run the build using mvn then to script the deployment in a more natural language.
The intention of Maven is building not deployment in the sense to production. For this purpose i would recommend things like Chef or Puppet. From a technial point of view it's of course possible to handle such things via Maven. What also possible to build on CI solution like Jenkins. Furthermore it's possible to run a script from Jenkins to do the deployment on production.

Use Maven to customize and build another Maven project

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.

Automatically download and run standalone Java app from Maven

I would like to have an application/script that I could use to execute a stand alone Java application (i.e. a basic class with a main method) from a given Maven artifact. Based on my research nothing like this currently exists, but I thought I'd ask the question to the community and see if anyone knows of a project that could support my needs.
The idea would be that I would pass in, either via a config file or the command line, a Maven artifact spec (group ID, artifact ID, and version) along with a fully qualified class name and a list of arguments. The given artifact and all of its dependencies would then be downloaded (or not if they're already in the local repo) and the application would be launched with the appropriate classpath.
At this point I don't think it will be too difficult to implement a system that does this using the Aether library and Java's ProcessBuilder, but I was wondering if anyone else knew of an existing project that already handles this before I start reinventing the wheel.
Perhaps this is obvious, but why wont the exec plugin work? http://mojo.codehaus.org/exec-maven-plugin/
mvn exec:java -Dexec.mainClass=com.project.App
If com.project.App were part of another jar, you would include it as a dependency like you normally would.
Running an application using Maven is pretty simple. I wonder if this something that you are looking at or did I get the question wrong?
mvn exec:java -Dexec.mainClass="com.test.foo.Main" -Dexec.args="arg1 arg2..."
I have just pushed to Maven Central the Installation plugin. It allows installing and executing artifacts from repositories and it doesn't require a project.
It's very similar to package managers like yum, apt or gem:
mvn installation:install -Dartifact=groupId:artifactId
This will create a shortcut to your class available on the path.

Invoke Maven-Module Build from IntelliJ

I was wondering if someone knows a way to invoke a specific Maven module build from IntelliJ that will also build (or use already compiled classes from) modules on which it depends.
So for instance if I would like to only build the module "Model" in the picture, it seems reasonable for me to click the package step on it. But what it actually does it invokes the mvn package step inside this specific module rather than mvn -am -pl module-name from the root module, which also builds all the dependencies.
So is there something that I just don't know?
removed dead ImageShack link
I do it this way (Idea 8.1.4)
Open run dialog (shift-f10 on windows)
Click + and pick Maven to add a Maven build config
Fill in the form, adding
1 working directory
2 maven command line options
3 maven goals
4 profiles
This is no different than running from command line. Which is what I am assuming you want.
You know the mvn command line well! To put this command line into IDEA, right click on your root project, one of the options will be "Create ... [package]".
Make these edits:
set the name to something like "Make Model [install]"
In the goals box, enter your command line, i.e. "-am -pl model install"
I know these aren't strictly goals, but it seems they are passed directly to maven. (Tested in IDEA 9.0.1)
When you run this command, it will now make Model - and the modules it depends on. Equally useful is the "-amd" to make all dependent moduels to verify that changes to Model haven't broken other modules. Having to create Run commands is not quite as elegant as the built-in UI, but you can associate these commands with hotkeys for quick access.
Hope this helps!
mdma
PS: A small aside. Package is currently your default build goal - this may not work as intended with multi-module builds, since the latest built classes are not accessible to dependent modules - using install as the default goal is recommended to ensure the most recently built versions of artefacts are used by dependents. If you're concerned that installing might add a broken artefact to the local repo and break all your local builds, use a separate local repo for each project (use a custom settings.xml.)

Categories

Resources