Specifying maven options when running maven lifecycle goals in Intellij - maven

I have an Intellij maven project and am able to use the View|Toolbars|Maven Projects , then select a specific maven module. From there i click on some lifecycle components, e.g
clean compile package
and run them. So far OK. But I need to add some maven options. What are my choices here? Do i need to set a static MAVEN_OPTS environment variable? The downside is it would be applied equally to ALL invocations of maven.
Is there any way to apply maven options e.g. -DskipTests to a single maven run as part of selecting the lifecycle targets on the GUI?
UPDATE
There is actually a nondescript icon for "Toggle skipTests mode" on the maven toolbar. However we have other settigs to include on the maven command line so that icon is insufficient.

The solution is to create a Maven run configuration. There are two ways to do so:
Run - Edit Configurations - + - Maven, or
Right click on your goal in the Maven view and select Create {goal}... (see screenshot:)
In the opened Run Configuration dialog use the Command line field to specify any command including arguments like -DskipTests

Related

Building maven project with IntelliJ doesn't create JAR

When running the following command in the terminal, I can see in the target directory the JAR file, whereas when building with IntelliJ I only see the compiled classes.
Why?
To get the jar built by IDE you need to configure the jar Artifact to build:
If you want to build your project and execute run actions through Maven, you need to delegate all those actions to Maven with the Delegate IDE build/run actions to maven option located in the Maven settings.
Build a project with Maven #
Click icons general settings svg in the Maven tool window. Alternatively, from the main menu select File | Settings/Preferences | Build, Execution, Deployment |Build Tools | Maven.
Click Maven and from the list, select Runner.
On the Runner page, select Delegate IDE build/run actions to maven.
more detail see here

creating different types of projects using eclipse with maven

I know creating the project using maven with command prompt but if i want to import this project into eclipse i have to run some commands and it will be modified suitable for eclipse, My question is can I create different archetype projects using maven plug in to eclipse, without using maven with cmd ?
You can entirely use Eclipse without using the command prompt. Though I prefer to use both terminal and Eclipse interfaces to utilize the maven project, and is a dynamic way of development. Eclipse Mars already include Maven.
Two ways to do this:
Create a new general project, create new POM file, define dependency and build, and Eclipse will recognize to configure as Maven project without applying Eclipse Project facets.
Create a new Maven project, do not skip archetype selection and use quickstart only if creating a simple Java project with main and test source folders, define module properties (group, artifact, version, etc.), and Eclipse will configure project as Maven project without applying Eclipse Project facets.
To execute a clean install, you need to create a goal "clean install" in Eclipse Run Configuration under Maven. Caution, Eclipse use embedded Maven runtime by default so if you'd like to link with your copy of Maven, you'll need to configure Eclipse to point at your Maven installation directory.
Basically, every command you entered in command prompt need to be a goal in Eclipse Maven Run Configuration to separate yourself from using command prompt.
Example Java Maven Project:
Step 1: Create New Maven Project
The first step to begin Maven-enabled Java development without using command prompt.
Step 2: Eclipse Project Configuration
Most of the time, I usually skip this section unless special circumstance requires a working set.
Step 3: Specifying Archetype
Maven archetype quickstart comes with two source package: test and main. This is the most simplest and efficient option to begin Java development. This is equivalent to -DArchetypeArtifactId=maven-archetype-quickstart option.
Step 4: Define Archetype Parameters
Define your own archetype parameters.
Step 5: Confirm Eclipse generated a Maven-enabled Java project
Double check POM and ensure Eclipse throws no error. In this case, Eclipse warns of Java 1.5 not defined. You can fix this by specifying Java version in maven-compiler-plugin within build tag in POM but that's entirely another thread.
Step 6: Define a Maven Goal
We want to test whether Eclipse can do a "mvn clean install" by creating a new run configuration. You can see the console output in background that Eclipse successfully output Maven build.
Is this what you were asking about?

How does IntelliJ's "Reimport All Maven Projects" button work?

How does IntelliJ's "Reimport All Maven Projects" button actually work?
I'm asking this because I see that IntelliJ's reimport button's behavior is different than that of running the mvn command with the -U argument, and it's not clear to me why.
I think you miss the part with local and remote repository.
If you run mvn -U it forces maven to download all libraries from remote repository that will be your company nexus or maven repo. The main difference with -U and without is that -U will override your local SNAPSHOT jars with remote SNAPSHOT jars. The local SNAPSHOT jars came from install and remote came from deploy command.
There will come the confusion with reimport. Reimport will load your local jars instead of remote jars, especially SNAPSHOT ones. You can enable force update snapshot in maven menu, which will enable -U switch in intellij.
Also double check that you share same .m2 directory with your system maven and intellij maven. You can see that in user setting file and compare it with command line maven. Just run mvn -X and check the settings section.
[DEBUG] Reading global settings from /usr/local/Cellar/maven/3.3.9/libexec/conf/settings.xml
[DEBUG] Reading user settings from /Users/xbaran/.m2/settings.xml
[DEBUG] Reading global toolchains from /usr/local/Cellar/maven/3.3.9/libexec/conf/toolchains.xml
[DEBUG] Reading user toolchains from /Users/xbaran/.m2/toolchains.xml
[DEBUG] Using local repository at /Users/xbaran/.m2/repository
asker's note for future readers
This answer did not fully explain the reimport button's behaviour, so I'm going to add my observations here for posterity.
There seems to be something going on with IntelliJ's caches (i.e., the .idea folder). using mvn clean install -U in terminal works as described here, but does not affect IntelliJ IDE; missing symbols are still described as missing. However, if I press the "magic reimport button", those missing symbols successfully resolve.
I can only assume that the reimport button is basically instructing IntelliJ to refresh its own cache, which is why there is a difference in behaviour.
If you changed the pom.xml file, IDEA needs to update the project structure.
For example if you've added there some more dependencies, IDEA needs to add them as project libraries.
So "Maven > Reimport" is used exactly to that - to reimport a maven module. It does not trigger any maven commands with any arguments.
"Reimport All" does the same but for all maven modules in the project.
As a side note, instead of using those two actions, in "Settings > Build, Execution, Deployment > Build Tools > Maven > Importing" you can choose "Import Maven projects automatically". This will automatically invoke "Reimport" action when the pom.xml is changed.

How maven pom.xml file convert configurations into system properties?

As we all know, suppose we define 'transportation' in pom.xml in maven,
after we run command like 'mvn jetty:run' the property 'db.name' can be discovered by other configuration, like 'hibernate.cfg.xml'. That's fine.
But now, I configure this project to run on tomcat directly , by 'run on server' from eclipse, instead of by maven command. (I did this by configure a project facets in eclipse, make it can use 'run on server' menu). You can see now I just run with eclipse, nothing to do with 'maven' or 'pom.xml', but it still use properties like 'db.name'. If I need change to another db, I had to change the 'db.name' property, then run some command in maven to make it take effect.
Can any body give some explanation about how it works? Does maven command generate some file at some place that takes the properties?
Properties are set for virtual machine. Maven run new/existing (dependent on configuration) instance of runtime environment. So you can pass properties as
-Dkey=value (or <key>value</key> in pom)
Check in eclipse configuration if you can pass arguments when you run tomcat?
Eclipse maven plugin (from Eclipse foundation) understands the pom.xml and usually syncs with any changes.
But sometimes I observed that it goes out of sync, and to fix that problem I need to delete the temporary folders/files created by Eclipse (.settings, .classpath and .project) and import again.

Debug a maven plugin's execution in a maven web-project

Is there any way to actually debug a maven plugin while it is in action. What I mean is that for example we have the maven-clean-plugin. So when this plugin executes it's action can we somehow debug and check inside the source code of maven-clean-plugin?
Obviously we would have to associate the Java source for the plugin in eclipse but how can we set it for debugging?
Thanks.
EDIT: Changing the subject
I'm sorry guys maybe I should have been more precise. Actually I have my web-app which is a maven project, which makes use of 3rd party maven plugins. Now when I do a mvn clean install I need to debug my 3rd party maven plugin. Now in my maven dependency I dont get a dependency to that plugin jar, which is quite normal. Any ideas?
If you are using Eclipse with the m2eclipse plugin, simply launch your build with Debug As... instead of Run as....
If you are not using m2eclipse, run mvnDebug instead of mvn (for Maven 2.0.8+) and attach a remote debugger on port 8000. For Maven 2.0.8<, add the remote debuggin options to the start script.
Of course, you need to import the sources of the plugin in your workspace.
See also
Developing and debugging Maven plugins
Dealing with Eclipse-based IDE
If you want to debug Maven execution in eclipse, here is how I did it, with mostly command-line tools (no Eclipse plugin used) (may be off at some points, I haven't done that for 6 months):
Run, from the command line, mvndebug in place of the mvn command. Maven will begin launching and wait for an external debugger to appear on a TCP port before resuming. Note the port number.
Configure in Eclipse a custom, remote debug configuration. (See http://www.ibm.com/developerworks/library/os-ecbug/ , Remote debugging) - set the port number as the one used by mvndebug. Also put the source files that you will be using for debugging in the Debug configuration.
Launch the remote debug configuration. Maven should resume and you will catch bugs in Eclipse.
This might be slightly off, since I'm going to talk about IntelliJ IDEA. With IDEA, you can load a maven project directly, and then simply right click on one of the build lifecycle phases (clean, package, install etc...) and choose debug. The IDE then runs that phase with the correct classpath and drops you into a debugger.
I've used the debugger to debug several of my own plugins. I've never tried debugging a third party plugin, although I imagine this will be relatively painless if the jar still has debug symbols in it and you have corresponding source code.
The community edition of IDEA is available for no fee.
Just replace mvn with mvnDebug in your command:
mvnDebug clean install
As result JVM listens for debugger at port 8000. For finer control you can set MAVEN_OPTS environment variable with standard JVM debug parameters:
export MAVEN_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
mvn clean install
In NetBeans 7.4, Right-click the project > Properties > Actions. Pick the Build project action (or any other), then next to "Set Properties" click Add and Debug Maven Build.

Resources