maven eclipse checkstyle plugin - eclipse

I have custom checkstyle checks file (called checks.xml), and I'm trying to use that same file in both maven and eclipse. It all works well, except for the SuppressionFilter.
In this checks.xml file, I have
<module name="SuppressionFilter">
<property name="file" value="src/main/resources/checkstyle/checkstyle-suppressions.xml"/>
</module>
This works when I run through maven. However, when I run through eclipse, I need to change the config to be
<module name="SuppressionFilter">
<property name="file" value="${basedir}/src/main/resources/checkstyle/checkstyle-suppressions.xml"/>
</module>
If I run with the ${basedir} property with maven though, I get the error that property ${basedir} has not been set.
Is there a way use this same configuration file in both maven and eclipse? I feel like there should be, but I'm just missing something on how to properly populate the suppression filter.
thanks,
Jeff

This is hell. Eclipse and Maven handle suppressions different and don't share variables.
Derived from Rolf Engelhard
So in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.8</version>
<configuration>
<propertyExpansion>config_loc=${basedir}/src/main/checkstyle</propertyExpansion>
<configLocation>${basedir}/src/main/checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>${basedir}/src/main/checkstyle/suppressions.xml</suppressionsLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Now in checkstyle.xml (${config_log} is an Eclipse specific thing, but by specifying it in the pom we make it available to maven as well):
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/suppressions.xml" />
</module>
And if you're using maven-site-plugin or any other plugins that also rely on CheckStyle don't forget to update those to have the config_loc property as well (or declare it global to the pom, though I wasn't able to get this to work properly).

Sure there is a way to use the same configuration file in both maven and eclipse but it requires a little setup first. I wrote a blog post on how to achieve this even for a multi-module maven project. see: maven-checkstyle-and-eclipse

<propertyExpansion>basedir=${session.executionRootDirectory}</propertyExpansion> works for me, but only when added to the <plugin>node, not to <execution>!
project.basedir does not work well in multi-module projects, because it will point to the submodule folder instead of the root folder.

You could try defining ${basedir} as a property in your pom.
See the pom reference quick overview.
<property>
<name>basedir</name>
<value>${project.basedir}</value>
</property>

Related

Checkstyle config used by Maven and Eclipse

I am try to use the same Checkstyle configuration file with both Maven and Eclipse. The module SuppressionCommentFilter works as expected in Eclipse, but Maven reports
TreeWalker is not allowed as a parent of SuppressionCommentFilter
If I move it from TreeWalker to Checker the error goes away, but Checkstyle does not process the ignore comments. I am using Checkstyle 8.12 with Eclipse, but have not found a way to use other than 6.18 with Maven. Since this module had been part of Checkstyle since 3.5 I don't see how that could be the problem. Any suggestions what is wrong?
<module name="Checker">
<property name="severity" value="warning" />
<module name="TreeWalker">
<property name="tabWidth" value="4" />
<module name="SuppressionCommentFilter" />
Mark asked what the looks like, so here is that piece of code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>verify-style</id>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
The pom and Checkstyle config file are all working correctly other than the
If it is a child of Treewalker I get the error from the Maven plugin, but not from the Eclipse plugin, however I can only get it to work with the Eclipse plugin.
I am using Checkstyle 8.12 with Eclipse, but have not found a way to use other than 6.18 with Maven
You must override the dependency in maven to bring in newer versions of Checkstyle. By default, the maven plugin will only use 6.18 but it can be overridden for newer versions.
See https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html
It appears that you have defined checkstyle as dependency and not as plugin.
Using the guide at Apache Maven Checkstyle I was able to use checkstyle for my personal projects. This is a snippet from the plugins section of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>verify-style</id>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<logViolationsToConsole>true</logViolationsToConsole>
<configLocation>yourcheckstylexmlhere.xml</configLocation>
</configuration>
</plugin>
Maskime on GitHub says:
Dear reader if you googled your way here, like I did, the solution is
to move the <module name="SuppressionCommentFilter"/> under the
TreeWalker module.
I have tried this fix with the version you're using aswell and it does not give the error anymore.

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>

Only copy modified files in maven-war-plugin

I'm currently using maven 3.x with the maven-war-plugin. For developer builds I would like to be able to use the war:exploaded goal, but only copy resources that have changed. Is there a way to do this?
I've been looking through the docs and have not been able to find a way to do this in the current versions, but there used to be (in the old maven 1.x version) a property maven.war.resources.overwrite that would allow this.
Thanks.
I'm not aware of a way to do this using the maven-war-plugin, but if your IDE supports it, you could have the IDE auto-deploy changes. For example, the Web Tools Platform for Eclipse supports this feature for Tomcat. However, if your build process is complex or does something weird before invoking the maven-war-plugin, this may not work for you.
A second option: if you're running Linux, set up rsync to copy recently modified files to your servlet container. A co-worker did this by having the servlet container's web app directory sync with the Eclipse project's output directory, and it worked well (just modify your code, Eclipse will build it automatically, and rsync will copy your changes).
For this purpose I use maven-antrun-plugin
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
....
....
<!--DJ: Settings for deploy only-->
<!--DJ: Dir to where copy files-->
<!--DJ: And date when build have been started, to select only modified files in the future-->
<properties>
<tomcat.dir.rootDir>C:\apache-tomcat-6.0.35\webapps\ROOT1</tomcat.dir.rootDir>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
.....
<!--Ohter dependensies here-->
.....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copyModifiedFilesFrom_ExplodedWAR_Dir</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Upload new files to dir ${tomcat.dir.rootDir} modified after date ${maven.build.timestamp} "/>
<copy todir="${tomcat.dir.rootDir}">
<fileset dir="${project.build.directory}/${project.build.finalName}" includes="**/*">
<!-- Include only recompiled files -->
<date datetime="${maven.build.timestamp}" pattern="yyyy-MM-dd HH:mm:ss" when="after"/>
<!-- and only *.class files -->
<filename name="**/*.class"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
.....
.....
</plugins>
</build>
</project>
Then run maven with params:
mvn pom.xml compile install org.apache.maven.plugins:maven-war-plugin:2.1-alpha-1:exploded
As result only changed files will be recompiled and only recompiled filed will be replaced in tomcat webapp dir.

How do I get Eclipse to resolve classes generated with Maven 2?

I'm using Google Protocol Buffers to generate some Java classes for my project. Using Maven 2 and its "antrun" plugin, these classes are freshly generated before compile, output to target/generated-sources and put on the classpath during the build. So building the project from the POM is no problem.
However, Eclipse doesn't know how to resolve the generated class, because the folder it's in doesn't seem to be on the IDE's classpath during development. I'm using m2eclipse and have it manage dependencies for me, so I had expected Maven to take care of this.
How can I get IDE support (code completion etc.) for the generated code?
m2eclipse supports this. First, add the path to your build path:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Second, add support for that to m2e:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<versionRange>[1.0,)</versionRange>
<goals>
<goal>parse-version</goal>
<goal>add-source</goal>
<goal>maven-version</goal>
<goal>add-resource</goal>
<goal>add-test-resource</goal>
<goal>add-test-source</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
The second step might not be necessary, if your eclipse installation has installed the "org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml" plugin. This plugin is available via Window -> Preferences -> Maven -> Discovery. Currently, that does not work here at Eclipse Kepler, therefore, I fetched the JAR (linked from the xml shown in the Catalog URL) and extracted the fragments from org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml by hand.
m2eclipse doesn't support this. You must manually add the folder target/generated-sources as a source folder. When you tell m2eclipse to "Update Project Configuration", this will be overwritten and you have to restore it.
Also, make sure that Eclipse looks for changes in the workspace.
There might be some issues, though. Eventually, you'll run into errors that some class can't be compiled because some other class can't be resolved. Code completion will work, though. The root cause of this issue is that Eclipse gets confused when Maven changes class files in target.
To solve this, you must tell Eclipse to compile to a different place than Maven.
What you should see in your project explorer is a container named "Maven Dependencies" in place of the usual "Referenced libraries". This means m2eclipse is managing your build path.
In my case, to achieve this, I checked "Include Modules" and unchecked "Skip Maven compiler plugin when processing resources" on the "Maven" section of Project->Properties.
Personally I resolved this problem by setting up the generated classes as a seperate project and made it a dependency in my main (non-generated) project. I was using wsdl2java to generate webservice classes so the "source" in my sub-project was the wdsl and xsds. Worked well even when the wsdl was changing regularly.
I had this issue with code generated using Maven and wsdl2java and here's what I did in Eclipse Juno to resolve it. Assume my project is named project1:
Right-click project1 and select Properties
Choose Java Build Path from the left and select the Libraries tab
Click Add Class Folder
Select the bin directory and click OK (project1/target/generated-sources/bin)
Click OK and Refresh the project
As an added bonus you can also attach the source code:
Click the arrow next to the new class folder you just created
Click on Source attachment
Click the Edit button
Set the Path to /project1/target/generated-sources/axis2/src
Click OK
Right-click project and select Properties
Choose Java Build Pathfrom the left and select the Source tab
Click Add Folder
Select the bin directory and click OK
(project/target/generated-sources/xxxx) Click OK and Refresh the project
How can I get IDE support (code completion etc.) for the generated code?
Typically I would add the m2e lifecycle-mapping plugin to the pom.xml file as described in #koppor's answer. However adding per-eclipse code to my pom.xml files is not an option at work which is mostly an IntelliJ shop.
My solution first adds the build-helper-maven-plugin to the pom.xml which works fine from the command line but not in eclipse.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
To fix eclipse I installed the Apt M2E Connector from the Eclipse Marketplace. I think things started working right after I restarted and then rebuilt all of my projects. I now see the following in my source dirs:
src/main/java
target/generated-sources
...
Feature!
Did you try to refresh the Eclipse project?
(source: oyvindhauge.com)
When an external tool generate new files or updates old ones, Eclipse will not be able to detect the change until the next request.
Another option would be to define a new Custom builder, specifying for that builder to "refresh resources upon completion":
alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
Worked for me (But you will to have to follow this every time so you can add this path in pom.xml)
Right click on your project > Build Path > Configure Build Path
In sources tag, click on [Add Folder] button
Check target/generated-sources/annotations
To generate Java source files from .proto files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.
Basic usage (see here for detailed description):
make sure that native protoc compiler is installed on your system
update your pom.xml file:
make sure you use at least Java 6 (Java 7+ is recommended)
add plugin invocation
add the corresponding dependency for com.google.protobuf:protobuf-java
put your .proto files inside project's src/main/proto directory
update the project (via Maven -> Update project...)
Example pom.xml:
<project>
...
<build>
<plugins>
<!-- Require at least Java 6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Generate .java files from .proto definitions -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
...
</dependencies>
...
</project>
Some additional notes:
if protoc executable is in the PATH the protocExecutable configuration entry can be omitted
test-only protobuf message definitions can be put into project's src/test/proto directory
I recommend installing Protocol Buffer Descriptor Editor (marketplace link)
Good luck!