Is there a way to let eclipse automatically run 'mvn install' when it builds a project (e.g. when a source file in the project is changed)?
My problem at the moment is that I have a large number of projects in eclipse, each of which is a Maven project as well. Projects depend on each other through Maven dependencies but are not parent/child projects.
My workaround is that I have a bash script running in the background which checks for changes in all directories for these projects and triggers 'mvn install' for every changed project. Unfortunately, this creates some problems since for some projects I have to call 'mvn clean' as well and this makes the eclipse projects unusable until I manually rebuild the project in eclipse (Maven -> Update Project ...).
Did you try Eclipse builders?
You may attach a builder to your different projects.
The builder will call your bash script each time its related project is built.
See the following article for reference: Non-Ant project builders
Related
I try to build a project, which is using the Maven Wrapper. The build works fine on command line. There is also a plug-in for IntelliJ Idea. However, I haven't found any article, how to build such project with Eclipse.
Do you have an experience with project build by ./mvnw command instead of mvn in Eclipse, please?
As implied by the comment from LMC, the Java edition of Eclipse will come with the "m2e" plugin, which will automatically recognize a project with a pom.xml file as a Maven project, and it will automatically note the dependencies, download them, and compile your source code with those artifacts as dependencies.
That deals with dependencies and compilation. If your build is running unit tests with Surefire, or generating code with particular plugins, unless there is a specialized m2e connector for that plugin, Eclipse will not know to do that.
Inside Eclipse, if you need to run a Maven build, you don't need to care about "mvn" vs. "mvnw". Eclipse will have an embedded Maven installation.
Case in question: A Maven based project that has a Maven Run Configuration with its 'Command line' parameters defined as clean install.
Clicking the green arrow to invoke 'Run my_maven_config' downloads all necessary packages from Nexus and completes the build successfully.
Selecting 'Build Project' however, invokes a seemingly similar process but after building successfully for quite a while, it fails on a missing Hibernate package.
Why is this difference?
Shouldn't the build part of 'Run' be identical to the "standalone build"?
They are not identical. IntelliJ IDEA imports the external project model from Maven and converts it to the internal project model. It's not 100% accurate, some Maven plug-ins are not supported, some dependencies may fail to resolve, etc.
See this answer to perform the diagnostics. Try re-importing the project (delete .idea directory first if it doesn't help).
Delegating build/run actions to Maven as it can be done now with Gradle will be supported later (probably in IntelliJ IDEA 2018.3).
Now IntelliJ IDE allow to delegate Build actions to maven.
Go to Preferences and search for "Delegate IDE build actions to maven".
So when you build a project, IntelliJ IDEA invokes the appropriate Maven goals.
In IntelliJ 14.1 what exactly happens when Build > Rebuild Project is invoked for a maven project.
Does it call mvn compile?
Does it do something independent of maven?
I have tried to testing this and when I click Build > Rebuild Project I can't see any console output indicating that maven is running, if I run maven from the maven projects view I get maven output.
So what exactly is the relationship between IntelliJ Build > Rebuild Project and build tools such as maven / gradle / ant?
No, the IntelliJ IDEA make does not invoke Maven. When you open a Maven or Gradle project, it reads the settings of the project and stores them as part of its internal project model. When you invoke Build, it uses the imported settings to compile your project using its own build system. Rebuild Project keeps the existing imported settings, deletes all .class files and IntelliJ's incremental compilation caches, and performs a full build of the project using IntelliJ IDEA's build system.
The advantage of doing that is that the IntelliJ IDEA build system supports very fine-grained incremental rebuilds (for example, if you change the signature of the method, it will recompile only those classes which actually call the method, and not any other code).
Note that you can configure IntelliJ IDEA to run an Ant task or a Maven goal either before or after the main project compilation, so if your build is doing something non-standard, you can still use it with the IntelliJ IDEA build system.
I imported a Maven project into Eclipse. I was able to deploy it from the command line. In Eclipse it is full of red Xs. I have cleaned the project, updated the configuration and I ran the following command:
mvn -Declipse.workspace=<pathtoworksapce> eclipse:add-maven-repo
and I still have the same problem. The project is dependent on another project.
What I ended up doing was removing all of the projects in Eclipse, but not deleting the source files. Then instead of directly importing the code as Maven->Check out as Maven Projects with SCM, I selected Existing Maven Project and imported the code from my local machine. This got rid of the errors, but when I would right click on code and select "Open Declaration", I would get Error: Could not open the editor: The file does not exist" So I removed all projects again and this time I deleted all the ".project" files and repeated the import Existing Maven Project. So now there are no red Xs (dependency problems) and the lookup functionality within the code works.
Go to your master project and type the following :
mvn install
mvn eclipse:eclipse
After that, import your project. You can also install m2eclipse for better support. If you use gwt or any other framework which has a custom maven execution, you should also install plugins for those in Eclipse.
I've got project with a few sub modules.
mvn clean install
works absolutely fine. But in Eclipse with the parent project open none of the maven dependencies are available and so all my Java source files are full of indicated compilation errors (they're not listed in the build path for the project). If I open one of the sub modules as a project there are no indicated compilation problems.
I've tried running
mvn clean install eclipse:clean eclipse:m2eclipsemvn clean install eclipse:clean eclipse:m2eclipse
and choosing project / update maven dependencies. Refreshing, closing and opening the project and Eclipse, etc.
Any ideas? Thanks in advance.
Getting m2eclipse to match maven's classpath
Just use Import... > Existing Maven Projects and import your multi-modules Maven project, that's all.
But in Eclipse with the parent project open none of the maven dependencies are available and so all my Java source files are full of indicated compilation errors (they're not listed in the build path for the project).
A parent project (i.e. with a pom packaging) is not a Java project, it's just there as a facility to make editing the parent pom.xml from the IDE possible (and that's already nice, importing nested projects inside Eclipse was initially not even possible).
If I open one of the sub modules as a project there are no indicated compilation problems.
That's how you are supposed to do things.
I've tried running (...)
You are not supposed to use the Maven Eclipse Plugin when using m2eclipse and this is not supported. Either use one or the other. If you decide to use m2eclipse, use Import... > Existing Maven Projects as suggested.