How to trigger command after build? - deployment

Is there a way for maven to trigger system command after successful or unsuccessful build?
Right now I'm using external script to run mvn package, but I would like this feature integrated with maven process.

Maven supports pre and post scripts. See the accepted answer here. In your case it would be mavenrc_post.bat
You can easily do that by creating a file at /etc/mavenrc or
~/.mavenrc (Linux) or %HOME%\mavenrc_pre.bat (Windows) Maven executes
those on startup if they exist unless you've defined a variable named
MAVEN_SKIP_RC.

Related

Running Maven commands on the command line in Eclipse

I am trying to use Maven to export my project into a JAR file, however I am also trying to export the dependencies as well and I am using the shade plugin, however I am wondering how exactly would I then run it?
I read I need to run "mvn shade:shade", however I don't exactly know where I run this? in the terminal I get 'mvn' is not recognized as an internal or external command, operable program or batch file.
So I don't really know where I am supposed to run this
Note: This solution assumes that your project is configured/imported as a Maven project.
Right-click the project in Eclipse. Select Run as > Maven build... > Goals, type package and then click Run.
Following best practices, you should not run shade:shade from the CLI (which you are actually doing with this approach), but rather bind the shade plugin to the package phase.
This link will explain how to do that, if you haven't already done it.

Force the use of an alternate POM file in eclipse

In my maven project I usually use the POM file named pom.xml. I have a second POM file though (call it pom_alt.xml), which I occasionally use to perform a very different build of the same project. To do so I specify the -f option in the command line
mvn clean package -fpom_alt.xml
as suggested by man mvn:
-f,--file
Force the use of an alternate POM file.
Now, when I am coding in eclipse I usually need maven to use pom.xml, but sometimes I should code or debug while the other file pom_alt.xml is used instead. Is there a way to tell the eclipse maven integration to use that file? Currently I am temporarily copy-pasting from pom_alt.xml to pom.xml since I seldom happen to need that, but you can see that's not optimal.
I am with Eclipse IDE for Java EE Developers, Mars Release 4.5.0.
UPDATE
I could build from the command line or use Run As Maven build as in
Carlos Andres' solution, but ideally I would like to define a persistent setting, like a property or preference. This because I find nice if the POM file can be fixed while I am doing things like running as Java Application and test cases, or executing on a server. Processes that require a project clean or server restart are often triggering a maven build with the default POM.
Check that M2E - Maven Integration for Eclipse is installed on Eclipse. Once that is installed go to the project and press right click
Next, put the command that you want to execute
This option allow you to save the commands, and the next time all your commands will be saved.
To execute the command recorded go to

Eclipse run mvn with command parameter (e.g. --encrypt-master-password)

I never had to do this before but maybe someone else has.
I want to run "mvn --encrypt-master-password".
I'm on a machine with only Eclipse and m2e. I could of course download and set up maven on my hard drive with the appropriate JAVA_HOME envvars etc, but I figured I'd try to get it done quickly in Eclipse.
m2e quickly gives a "no goals have been specified" error.
I could put in a dummy goal but I can't see any way to enter a mvn command line parameter (as opposed to a 'goal parameter')
This is theoretical now since I'll go and do this on a command line somewhere, but I'd like to know if it's possible.
For a one-time password encryption job you can set --encrypt-master-password <your password> as a goal in the Maven build as already specified by flafoux (Run Configurations > Maven Build > New build > ...).
But this is not secure since you password is stored in a configuration (unless you delete it right after the execution). And this is also not handy since you need to update a job manually for every encrypted password you need...
ps: https://maven.apache.org/guides/mini/guide-encryption.html says since 3.2.1 Maven should ask for the password if not specified, but it does not at least at my eclipse lune having Maven 3.2.1 embedded - may be it does at more recent verions.
You can set goals with build configuration.
Create a maven build configuration (Run as > Run Configuration... Maven builds , and add goals.
more explanation : http://books.sonatype.com/m2eclipse-book/reference/running-sect-running-maven-builds.html

finding what commands are executed by eclipse

I have a project in eclipse right now that is compiled using ant. I am wondering what eclipse is doing behind the curtains whenever I double click on the jar target of one of this build files.
Is it possible to get the commands that eclipse is executing with ant, as I'd like to setup a shell script that compiles the project. How can I find what commands it is actually executing and what parameters it is passing.
I imagine there are also some variables like classpath, and buildpath that are set, where do I find them as to be easily copied over.
Eclipse comes with its own installation of ant. Is it possible to use that bundled installation of ant to build via command line.
Any help appreciated,
Ted.
One possible way is to look at the process executed by Eclipse when building.
For that, use Process Explorer (if you are on Windows) in order to display the full command line and all its parameter when running that build.

How to solve problem with out of synchronization of J2EE module dependency in Eclipse?

I have two projects : my-lib and my-web.
my-lib is built using the Java compiler of Eclipse, and a short Ant task is run on some properties files, which are then modified (as explained here).
This is not a pretty solution, but it works.
Now, as my-web is dependent of my-lib, I define my-lib as a Java EE Module Dependency of my-web.
However, when I attach my-web to the Tomcat in Eclipse, and try to publish, I get the following exception:
'Publishing to Tomcat at localhost' has encountered a problem.
Resource is out of sync with the file system: '/my-lib/target/classes/my-app.properties'
This file is indeed one of the files modified by the Ant task.
How can I correct this problem, as I need to have the Ant task run on the my-lib.
If you have the Ant task run as an external builder or external task, you can configure it to refresh the project, workspace, or selected resources after execution. When specifying the task/builder click on the Refresh tab and pick the appropriate one for your needs.
There's more information in the eclipse help.