Use checkstyle configuration from plugin when generating eclipse files from maven - eclipse

I have a common artifact where I store build-tools configuration files, e.g. checkstyle, pmd etc.
I can access the files and everything works as expected when I run from the console. The configuration files are included in my project and the reports are generated as expected when I run mvn site from the console.
However, files that are required by eclipse and should be included when mvn eclipse:eclipse is run are not to be found. I got an error telling me that the files cannot be found.
This is the important part of my POM.xml file:
<build>
<extensions>
<extension>
<groupId>com.foo</groupId>
<artifactId>build-tools</artifactId>
<version>1.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalBuildcommands>
<buildcommand>net.sf.eclipsecs.core.CheckstyleBuilder</buildcommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>net.sf.eclipsecs.core.CheckstyleNature</projectnature>
</additionalProjectnatures>
<additionalConfig>
<file>
<name>.checkstyle</name>
<location>${basedir}/eclipse-checkstyle.xml</location>
</file>
</additionalConfig>
</configuration>
</plugin>
</plugins>
</build>
So my question is how to get maven to understand that these files, which is located in my build-tools artifact, to be included when I run the eclipse:eclipse command?
EDIT: It is the eclipse-checkstyle.xml that cannot be found.

You should add the jar holding the relevant sources as a dependency of the plugin, not an extention:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
...
</configuration>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>build-tools</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>

Related

Maven only generates Antlr-sources in default package

I'll start with my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HPK</groupId>
<artifactId>WRB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WRB</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<executions>
<execution>
<configuration>
<arguments>
<argument>-visitor</argument>
<argument>-package</argument>
<argument>wrb.grammar</argument>
</arguments>
</configuration>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<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-clean-plugin
</artifactId>
<versionRange>
[3.0.0,)
</versionRange>
<goals>
<goal>clean</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
When I run maven install on this project, maven should generate the sources from the antlr4 plugin within the wrb.grammar package, but it doesn't. It does everything, but put the sources into those directories, it simply puts them in what it calls the "default-package", which is just the root of antlr/generated-sources.
If I use the Antlr4IDE-plugin by right clicking the grammar and selecting it under run as, the sources are generated in the right directory.
Another person I'm working with on this small project has no problem using maven-install. Besides our operating systems and eclipse versions, everything is the same.
I'm using Eclipse Oxygen on MacOS.
What am I doing wrong that the maven-plugin doesn't generate my desired directory?
I have checked the sources of antlr4 version 4.3. The -package parameter is only used by the code generator templates but not anywhere in the actual tool source code (seeGithub search results for genPackage). So it cannot have an effect on the location of the output files.
Instead, the location of each output file is determined based on the location of the corresponding input file (see here and here in the sources). This is fits with the explanation in the maven plugin docs:
If your grammar is intended to be part of a package called org.foo.bar then you would place it in the directory src/main/antlr4/org/foo/bar. The plugin will then produce .java and .tokens files in the output directory target/generated-sources/antlr4/org/foo/bar When the Java files are compiled they will be in the correct location for the Javac compiler without any special configuration. The generated java files are automatically submitted for compilation by the plugin.
Additionally, when using the antlr4-maven-plugin it is not necessary to specify the -package option. As the plugin derives the value of the -package paramter from the input file path and automatically adds it to the antlr invocation (see here in the sources). That is probably also the reason why -pacakge is not directly available as a configuration parameter in the maven plugin.
Solution
In order to have the generated files placed in a directory structure that matches your package names, you need to use the same structure for the input files.
Essentially, all you need to do is to put your grammar files in src/main/antlr4/wrb/grammar, remove the -package parameter from the configuration and it everything work as expected.
By the way: instead of writing
<arguments>
<argument>-visitor</argument>
</arguments>
you could simply write
<visitor>true</visitor>
since this parameter is directly understood by the antlr4-maven-plugin.

Importing Wrong Dependency Versions

I'm using Maven with Eclipse - my POM contains the dependencies:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.2.3</version>
</dependency>
In my build path I see a red "x" on maven-plugin-api-2.0.6.jar. I also see other jars that are using version 2.0.6 (maven-core, maven-project, maven-etc.) which seem to compile fine - I know importing the correct jar version would solve the problem, but why is eclipse trying to use an older version of maven on my project?
First a maven-plugin should never be used as a dependency. It should be configured as a plugin and NOT as a dependency see the following:
<project>
...
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
...
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
...
</plugins>
</build>
...
</project>

Is it possible to specify a different JVM to run Eclipse with tycho-surefire-plugin

We have a project which is built with Tycho 0.15.0.
When running the tests (i.e. UI tests), maven executes
cmd.exe /X /C ""C:\Program Files (x86)\Java\jre7\bin\java.exe" -Dosgi.noShutdown=false -Dosgi.os=win32 [...]"
This works so far.
But now, we want to have the test instance run with a different JVM (located e.g., in c:\my_custom_jvm\jre\bin).
Is this possible to achieve? I have searched for possibilities and found the jvm option for the Maven Surefire plug-in, but this does not seem to be supported by tycho-surefire ...
For reference, here's the complete snippet of the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>0.15.0</version>
<configuration>
<testSuite>my.tests</testSuite>
<testClass>my.tests.AllTests</testClass>
<product>my.product</product>
<bundleStartLevel>
<bundle>
<id>org.eclipse.equinox.event</id>
<level>4</level>
<autoStart>true</autoStart>
</bundle>
</bundleStartLevel>
<dependencies>
<dependency>
<type>p2-installable-unit</type>
<artifactId>my.product</artifactId>
<version>0.0.0</version>
</dependency>
</dependencies>
<argLine>-Xmx768m -XX:PermSize=128m -Xss1m -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook -Dequinox.ds.block_timeout=60000 -Dequinox.use.ds=true</argLine>
</configuration>
</plugin>
</plugins>
</build>
tycho surefire has support for maven toolchains [1]
[1] http://maven.apache.org/guides/mini/guide-using-toolchains.html
(based on answer by jsievers)
The toolchain plugin does exactly what I need.
I added the following lines to my pom.xml (inside the tag):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.4</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
And I have created a toolchain.xml file in C:\Users\itsame\.m2 (if you want it to place elsewhere, maybe this helps) with these contents:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>1.4</version>
<vendor>sun</vendor>
<id>CustomJRE</id>
</provides>
<configuration>
<jdkHome>c:\my_custom_jvm\jre</jdkHome>
</configuration>
</toolchain>
</toolchains>
Note that even though it is a JRE (not a JDK), this works to run the tests.

Maven generate-sources folder not being picked up by Eclipse

I am in the process of migrating my build from Ant to Maven. The Ant build used to compile a "code generator", execute this "code generator" which produced a set of Java and C code. It then took the generated Java code and compiled it along with some additional code produce a single jar.
I have replicated this this in Maven quite easily and it works well when I run from the command line but Eclipse is complaining and is giving me an error relating to the pom file
Failure to find {group.id}:{artifact.id}:pom:1.0.0-SNAPSHOT in
http://{our internal site repository}/nexus/content/groups/public was
cached in the local repository, resolution will not be reattempted
until the update interval of snapshots has elapsed or updates are
forced
where the group.id and artifact.id are the group and artifact id of my code generator plugin
and any code that references the generated code also fails to compile.
My maven build consists of
a generator project that contains just the Java code for the code generator.
a generator-plugin project that contains just the code to wrap the generator as a Maven plugin. This is dependent upon the generator project.
an xyz project that uses the plugin to generate the code. The code ends up in the target/generated-sources/xxx folder of this project. I have configured the build-helper-maven-plugin as per Maven compile with multiple src directories to include this extra source directory.
If I manually add the generated source folder to the Eclipse build path all of the errors relating to the code not being there go away on that project but not on any downstream projects and the "Failure to find..." error listed above remains.
What puzzles me a little is that it is referring to the ...:pom:1.0.0-SNAPSHOT when in fact my generator-plugin is defined as a maven-plugin.
Is this a sensible approach?
Why am I getting a "Failure to find..." error?
Why isn't Eclipse picking up my generated-sources folders?
I should also say I have the m2e plugin and the m2e connector for build-help-maven-plugin installed in my Eclipse IDE.
It looks like a problem during the download of the lib from the repository. I already had the same error message once.
Did you take a look at your local repository?
Go to the .m2 folder and look for /nexus/content/groups/public. If the folder is there, open it and see if the lib was download correctly. If not, try to delete the folder and try to run mvn install to force the download of the lib.
At Eclipse, run Right button > Maven > Update Project too.
Are you using an local repository like Artifactory, aren't you? Also look for the lib in the repo1-cache (or similar) folder. See if the jar is there.
Are you behind any firewall or proxy?
Using eclipse Indigo3.7, this was what I found that worked good using spring 3.1.1 which does have the 3.0.6 version as well in it. Once I got the plugins setup and put into the correct area of the pom and included the argline and endorseddirs to have the java sources put out into the target/generated-sources/cxf folder then maven generated the sources ok.
....
<properties>...
<dependencyManagement>
<dependencies>.....
</dependencyManagement>
<dependencies>
<dependency>....
</dependencies>
<!-- *************************** Build process ************************************* -->
<build>
<finalName>projName</finalName>
<plugins>
<!-- Force Java 6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Deployent on AS from console
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.as.maven.plugin}</version>
</plugin>
-->
<!-- wildbill added tomcat plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<!-- Surefire plugin before 2.9 version is buggy. No need to declare here,
it's being referenced below w/ the version
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
-->
<!-- developer added these -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>target/generated-sources/cxf</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>target/generated-sources/cxf</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2</version>
</artifactItem>
<artifactItem>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2</version>
</artifactItem>
</artifactItems>
<outputDirectory>target/generated-sources/cxf</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<!-- *********************** Profiles ************************************ -->
<profiles>
<profile>
<!-- When built in OpenShift the 'openshift' profile will be
used when invoking mvn. -->
<!-- Use this profile for any OpenShift specific customization
your app will need. -->
<!-- By default that is to put the resulting archive into the
'deployments' folder. -->
<!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
<id>projName</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>process-sources</id>
<phase>generate-sources</phase>
<configuration>
<fork>once</fork>
<additionalJvmArgs>-Djava.endorsed.dirs=target/generated-sources/cxf</additionalJvmArgs>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
<!-- Actual war created in default target dir -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
If your wsdl folder is in ${basedir}/src/main/resources it'll find it automatically
Hope this helps!

Unable to see Web Resources folder after running eclipse:eclpise for WTP project using maven

I see that m2e is able to create all the necessary artifacts for building my web application.
However the problem I'm facing is that I don't see the "Web Resources" section in the project explorer.
To navigate to my web resources(jsp, js, css ...) (which happen to be under src/main/webapp), I need to expand the folder structure everytime.
the following is my pom
<?xml version="1.0" encoding="UTF-8"?>
<project>
...
<packaging>war</packaging>
<dependencies>
...
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
</configuration>
</plugin>
</plugins>
</build>
</project>
How to I change my POM such that Eclipse maps src/main/webapps to "Web Resources"??
I'm using eclipse 3.7.0 and m2e 1.0.100
Any help is appreciated!!!
Have you installed the Maven Integration for WTP (a.k.a m2e-wtp)? It's available from the Eclipse Marketplace, or the m2e marketplace. See http://community.jboss.org/en/tools/blog/2011/08/01/m2eclipse-wtp-0131-back-to-the-m2e-marketplace for more informations on how to install it.
You can also find more informations on the Deployed Resources node (formerly Web Resources) in
http://community.jboss.org/en/tools/blog/2011/06/23/m2eclipse-wtp-0130-new-noteworthy
Just a shot in the dark: Right click on your project, Maven->Update Project configuration...
Or maybe you installed the wrong Eclipse version? Eclipse IDE for Java EE Developers should do the trick.