Running Maven commands on the command line in Eclipse - 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.

Related

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

Force Eclipse to clean a project automatically before every run

I'm looking for a solution that will force Eclipse to automatically clean a project before I run it (I'm talking about running a project using just Eclipse- no Maven, no Ant). For building I already have a Maven configuration, but sometimes I run the build directly from Eclipse as well and this is when I need that cleaning.
Shouldn't it be possible to have Maven and Eclipse use different class folders, e.g. /target for the Maven build and /bin for the Eclipse internal Java compiler? If so, you should be able to have 2 different launch configurations running the code from 2 different locations.
Second alternative: You can create a small Ant script to clear the target directory. That Ant script can be run from inside Eclipse, so a workaround is running the Ant launch configuration first and your Java launch configuration afterwards. To make this a one-step process, please install the launch groups feature from Eclipse CDT (you only need that small feature, not the whole CDT!), then you can create a "batch" like launch configuration from the other two launch configurations. Now everything is inside Eclipse with a single launch configuration!

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.

Refresh an Eclipse project with Ant

We have some Ant script fosr building the different jars we use, in several Eclipse projects. There is some interdependency
At the end of the build, we have to refresh some of the Eclipse projects in order to make the build path valid. Without this, it doesn't see one of the just-built jars and Eclipse throws a build path error.
There is a way to avoid the manual refresh step?
Try this:
<eclipse.refreshLocal resource="project_name/folder_name" depth="infinite" />
There are option details at the Eclipse help system.
Important: you have to choose "Run in the same JRE as workspace" at the Ant run configuration.
You may want to turn on "Refresh Automatically".
See Window->Preferences->General->Workspace->Refresh Automatically
and it'll monitor filesystem changes for you.
If you're running the ant script from within eclipse you can right-click on it and select "Run As >" then "Ant Build...". There's a 'Refresh' panel in the options to run the script which you can choose to refresh various things after the script completes.
This might not be answering your question exactly but I don't quite understand what is causing the problem.
As I understand it you have a project A that creates a jar file and that jar file is referenced by project B. When you re-build project A, you need to refresh so that project B can see the new jar file. Is that correct?
I do something similar but I don't get any build path errors because the name of the project A jar file hasn't changed.
Another option is for Project B to reference Project A directly instead of referencing its jar file.
I think I may have misunderstood the problem you are having but I hope this helps anyway.
it can be done by following Ant tasks provided by the Eclipse platform.
<eclipse.refreshLocal resource="MyProject/MyFolder" depth="infinite"/>
Note: make sure to run Ant inside the same VM where Eclipse workspace is running, for details check following links
Problem: failed to create task or type eclipse.refreshLocal
Ant tasks provided by the Eclipse platform