Automatically activate maven profile in Intellij IDEA - eclipse

Is there a way to automatically activate a maven profile when the project is opened in IntelliJ IDEA?
For eclipse, this can be done by using the property m2e.version for activation and I thought there may be something similar for IDEA.
<profiles>
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<directory>${project.basedir}/eclipse-target</directory>
</build>
</profile>
</profiles>
What I basically want, is to have a separate build directory for IDEA. This is useful to run mvn commands on the command line without messing with the IDE.

IntellJ sets the idea.version property when running maven run configurations. It also sets this property while detecting default profiles when importing a maven based project.
<profile>
<id>activeInIdea</id>
<activation>
<property>
<name>idea.version</name>
</property>
</activation>
<build>
<directory>${project.basedir}/eclipse-target</directory>
</build>
</profile>
After opening the project in IntelliJ the profile (in the example above called activeInIdea) is already pre-selected.

When imported as Maven project, IntelliJ will detect your maven profiles and import them as well. It will allow you to activate profiles on import and will even enable profiles which appear enabled during import. You can toggle the profiles after import in the Maven Projects tool window.
If your question aims on how to save you one click to activate the profile, then I haven't answered your question. But if you just missed the Maven Projects window, then here you go.

Related

Maven Incremental Compilation under Eclipse

I've recently converted a big (and ugly) legacy project to Maven. It is a really really huge project (~2.7M lines of code, and a nice mix of java, jsp, js, and even VBS scripts) and is not well structured. Everything is located under a single Maven artifact (and thus a single Eclipse project).
Even with that bad structure, it used to be less or more manageable from Eclipse (even if a bit slow), but now, since it is Mavenized, every single change made to source code triggers an almost full rebuild of the code base (and 2.7M LoC take something like 10 minutes to finish), which is really unusable on a daily basis.
I've found the following bug report related to maven-compiler-plugin, unable to have an incremental compilation: https://issues.apache.org/jira/browse/MCOMPILER-205, and I'm quite confident that this is the root of our performance issues.
Have you any workaround? Do you know of a maven-compiler-plugin version not impacted with that issue? Or a setting in Eclipse which could help? Any other idea?
Thanks
The incremental compiler in Eclipse (ecj, used by the JDT) totally overrides the compiler used by command line Maven builds. Any maven-compiler-plugin or javac issue should be discarded as irrelevant. I would expect m2e/Maven to have no significant impact on a single project workspace, as big as it is. Obviously since you're seeing the opposite behavior, something is apparently slowing down the Maven builder.
Since you use jsp and js files, my guess this is a war project. If you're using the Eclipse Java EE distribution, one thing you can try is to disable the m2e-wtp integration, see if it makes any difference. Go to Preferences > Maven > Java EE Integration and uncheck Enable Java EE Configuration.
If that doesn't help (or doesn't apply to your set up), another thing you could try would be to disable Maven resource processing during incremental builds. In your pom.xml, try adding:
<profiles>
<profile>
<id>m2e</id>
<!-- This profile is only activated when building in Eclipse with m2e -->
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-resources-plugin
</artifactId>
<versionRange>
[1.0,)
</versionRange>
<goals>
<goal>resources</goal>
<goal>testResources</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
There's a chance it might break something else, but at least that should help determine whether resource processing is the culprit here.
Anything else will require a deeper analysis, I suggest you open a ticket to https://bugs.eclipse.org/bugs/enter_bug.cgi?product=M2E
Disable automatic build is the only option you have. Just build when you are interested in the artifacts.
You did not say what goals you are calling maven with. Maybe it would already be enough if you do not run all tests using -DskipTests (or even not compile them by using -Dmaven.test.skip=true), or if not willing to skip those introduce profiles to run just the necessary tests.
Since you are telling it's a legacy project I suspect that changing the project structure into a multi-module maven project is out of scope. You can then try if you can speed up using parallel builds (using -T 4 (4 threads) or -T 2C (2 threads per core).

Log4j2 in Eclipse and Maven - need to have different config files

I am using Eclipse and m2e on my workstation.
Sometimes I run my unit tests individually with the built-in Eclipse JUnit functionality to display the results in the JUnit view.
At other times before checking in code, I run the whole test suite and integration tests using the Maven test goal.
However when I forget to change my log4j2 configuration to set the minimum log level to DEBUG, then the Maven process kills Eclipse and I need to restart.
I mostly set the log level to TRACE when I am running individual unit tests.
My log4j2 config file is in my src/test/resources directory.
I also have log4j2 set up in-container to read a config file on the file system.
Can I configure my Maven goal in the Eclipse Run dialog to use a different log4j2 file?
Turned out to be quite simple.
Create a new config file src/test/resources/log4j2-maven.xml
Configure the surefire test runner in the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemProperties>
<property>
<name>log4j.configurationFile</name>
<value>log4j2-maven.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

How to set eclipse project specific validation preferences with m2e

When invoking importing maven projects into eclipse from git using "Import Maven Projects" the m2e plugins to seems to generate a new .project and .classpath and some other files even though those files are checked in.
To get around this problem I would like to not check in any eclipse specific files, but there are customizations made to each project file such as custom save actions, or changes to the validation to ignore specific things, or changes to the javascript configuration for a dynamic web project that need to be preserved, so either those files need to checked or those settings need to be generated by m2e.
Any way to use m2e to configure the eclipse project validators?
We had a similar problem in the past. We solved it by
don't checkin eclipse specific files (svn ignore, git ignore, ...)
Use Workspace Mechanic plugin to keep eclipse settings the same accross development machines / developers.
The other answer has suggested the solution with eclipse. But if you are flexible with using a different IDE, I suggest using Netbeans with git. I tested both IDEs and finally found out I could save time in different cases using Netbeans! No such problems as mentioned above.
I made m2e-codestyle-maven-plugin for another purpose but it should serve what you need as well (in my case I wanted to set the default settings to have all validations turned on from my coding-standards project.
The key thing you have to do is fork a copy of my coding-standards project and add/change the appropriate setting in org.eclipse.wst.validation.prefs namely
vals/org.eclipse.wst.xml.core.xml/global=FF03
To disable the validations for XML for example. You can have it activated only on m2e by using profiles as shown here
https://github.com/trajano/jee/blob/master/jee-domain-api/pom.xml#L24
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>net.trajano.mojo</groupId>
<artifactId>m2e-codestyle-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>configure</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.trajano</groupId>
<artifactId>coding-standards</artifactId>
<version>${coding-standards.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>

Eclipse auto-build output interacts with Maven command-line build output

Since both use the target directory, Eclipse's build output sometimes interferes with the output of mvn builds run at the command line.
What's the best way to separate the two outputs?
Insert the following into your pom.xml. Eclipse's "m2e.version" property will activate the following profile which alters the location of the Eclipse build
<profiles>
<profile>
<id>IDE</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<!-- Put the IDE's build output in a folder other than target, so that IDE builds don't interact with Maven builds -->
<directory>target-ide</directory>
</build>
</profile>
</profiles>
Official way is presented here:
http://wiki.eclipse.org/M2E_FAQ#How_to_configure_Maven_project_to_use_separate_output_folders_in_Eclipse
I personally don't do something like this. Usually I basically disable auto-build in Eclipse since most builds I do from the console anyway. But if you really want it, here you are.
If you use maven-eclipse-plugin instead of M2Eclipse, here's the definition you want in order to change the Eclipse output directory:
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<buildOutputDirectory>target-eclipse/classes</buildOutputDirectory>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>

Can I access Maven properties as variables in Eclipse?

I have a Maven project that derives the path to a native library using properties which can be set in local profiles. For example:
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<LD_LIBRARY_PATH>${foo.libdir}</LD_LIBRARY_PATH>
</environmentVariables>
<fork>always</fork>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>foo-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<foo.libdir>/usr/local/lib</foo.libdir>
</properties>
</profile>
</profiles>
</project>
When I'm working with this project in Eclipse using the M2Eclipse plugin, I'd like to be able to set up run configurations that also reference this path in the same was (e.g., so I can run the Eclipse debugger on the project). Is there a way to access the Maven property from Eclipse, e.g., so that could set LD_LIBRARY_PATH in the run configuration using a variable like ${maven_property:foo.libdir}?
I do not think so.
Your maven script could refer to environment variable that you could define in the environment tab of your launch configuration
But the other way around (i.e. your configuration resolving at launch time some maven script property) is not, as far as I know, possible.