I am working on a project where we want to use the plexus-compiler-eclipse plugin during a Jenkins pipeline to check for increases in the number of warnings generated by the Eclipse compiler. We still want to use the javac compiler for the normal build and test stage, so I am trying to create a maven profile we can run during the warnings stage that utilizes the Eclipse compiler.
When I run the Eclipse compiler over our code, I get a compile error about JAXB dependencies being missing. I know this is due to our move to Java 11 from Java 1.8, but we do not get this error when building with the javac compiler. I have tried adding the jakarta.xml.bind-api dependency to the maven-compiler-plugin, but this does not help, nor does adding the org.glassfish.jaxb dependency or the javax.xml.bind:jaxb-api dependency.
I cannot share the full pom because this project is proprietary, but the profile I'm building looks like this:
<profile>
<id>eclipse-compile</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1></version>
<configuration>
<compilerId>eclipse</compilerId>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArguments>
<properties>${project.basedir}/.settings/org.eclipse.jdt.core.prefs</properties>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>ecj</artifactId>
<version>3.25.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
I was putting the various JAXB dependencies I tried in the <dependencies> section under the org.eclipse.jdt entry.
Anyone else encounter this or know what to do about it?
The issue stemmed from the Maven build running in Java 11 but our normal compile stage forking to a Java 1.8 executable. Because the Plexus compiler cannot fork to a new environment, there was not a way for it to access the Java EE dependencies. We just need to update our entire project to be compatible with Java 11 at compile-time.
Related
I used the org.openjfx:javafx-archetype-simple Maven archetype to start my first JavaFX project.
The resulting POM:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.invoicing</groupId>
<artifactId>Invoicer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release> 🡄 ???
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>com.example.invoicing.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
➥ What is the purpose of the <release>14</release> line in <configuration> for the plugin <artifactId>maven-compiler-plugin</artifactId>?
I found this documentation for the Maven Compiler Plugin, Compiling Your Java Sources. But it only mentions <!-- put your configurations here -->. So I do not know anything about specific configuration options here.
Is <release>14</release> the version of Java being used to compile? Is it the version of OpenJFX? Something else?
I tried using 28 arbitrarily. Doing a Maven > install threw this error with an unhelpful error message with no clue as to a release of what product:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project Invoicer: Fatal error compiling
The release flag is equivalent to specifying the source and target of the same value for the compiler plugin. It supports the -release argument for the Java compiler since Java-9.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
Note: For the same reason, you can get rid of the redundant properties declared as
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
Further: What is the --release flag in the Java 9 compiler? | The -release flag in javac was introduced to Compile for Older Platform Versions.
To complete the answer over the part where you've tried the version value as 28. While using Maven version -
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T00:30:29+05:30)
The error message reads very clearly what it should (if you could share the complete logs in the question)
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project forty-bits-of-java:
Fatal error compiling: error: release version 28 not supported
The compile options page states that this ends up as the -release argument passed to javac.
And about --release release:
Compiles against the public, supported and documented API for a specific VM version. Supported release targets are 6, 7, 8, and 9.
As I understand it, Java 9 introduced a feature that helps developers build on a recent compiler, targeting an older runtime, but at the same time preventing the old problem that let code compile with references to newer APIs while being targeted at old runtimes.
See: JEP 247: Compile for Older Platform Versions
Ex: If you use Java 8 to compile code that uses new Java 8 APIs (such as Collection.stream()), with a target of 1.7, this code compiles but will fail at runtime on Java 7 with a NoSuchMethodError.
On JDK 9, if you use --release 1.7, the compiler will know that Collection.stream() can't be correctly targeted at Java 7 and will fail the build.
tl;dr
In your POM, replace the two tags source & target as seen here:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--The following `source` and `target` tags are now replaced by `release` seen further down below.-->
<!--<maven.compiler.source>14</maven.compiler.source>-->
<!--<maven.compiler.target>14</maven.compiler.target>-->
</properties>
…with the new release tag, placed further down in POM:
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
…
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!--Enable Java language preview features. Specify Java version.-->
<!--This `release` tag replaced the pair of `source` and `target` tags seen commented-out near top of this POM.-->
<release>14</release>
</configuration>
</plugin>
…
…to tell the Java compiler the version of Java on which you intend to deploy. This tag passes a release flag to the Java compiler. The compiler errors out any of your code trying to use an API added to later versions of Java.
See another Question: What is the --release flag in the Java 9 compiler?
Details
Both Answers by Naman and by ernest_k are correct and important. But I need to write this Answer to combine them and show the direct solution.
Problem
The issue is that JEP 247: Compile for Older Platform Versions added a feature in Java 9 for a new compiler flag -release to replace the combination of older -source, -target, and -bootclasspath flags. This plugs a hole that plagued programmers trying to work on the latest compiler while writing code limited to making API calls of an earlier version of Java.
For example, I may be writing on my Mac using Java 12 yet deploying to a server running Java 8. I want the compiler to stop me from accidentally using features that arrived in later versions of Java. Otherwise, my app will succeed at compile-time yet fail at run-time when those features are unavailable on the older JVM.
To quote the JEP:
Summary
Enhance javac so that it can compile Java programs to run on selected older versions of the platform.
Motivation
javac provides two command line options, -source and -target, which can be used to select the version of the Java language accepted by the compiler and the version of the class files it produces, respectively. By default, however, javac compiles against the most-recent version of the platform APIs. The compiled program can therefore accidentally use APIs only available in the current version of the platform. Such programs cannot run on older versions of the platform, regardless of the values passed to the -source and -target options. This is a long-term usability pain point, since users expect that by using these options they'll get class files that can run on the the platform version specified by -target.
Solution
In a Maven-driven project, we pass those flags to the Java compiler by setting tags on our Maven POM file.
In your Maven POM file’s tag hierarchy of:
<project> …
<build> …
<pluginManagement> …
<plugins> …
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
…nest the following tag hierarchy, within which we specify our desired deployment version of Java.
<configuration>
<release>14</release>
By the way, if using a version of Java offering "preview" features, we can nest a further tag and value if we wish to enable those preview features.
<compilerArgs>--enable-preview</compilerArgs>
The old-school settings replaced by the new release tag were a pair of tags, source and target. These two could be set in Maven to be passed along to the Java compiler. So if you add the release tag seen above, check to see if your POM has this pair of tags. If found, delete them.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source> 🡄 Delete if using `release` tag.
<maven.compiler.target>14</maven.compiler.target> 🡄 Delete if using `release` tag.
</properties>
Example POM file
Here is a complete example POM file for Maven 3.6.3, for a basic Java project.
We are using all the latest versions of various plugins and dependencies. This example uses Java 15 with preview features such as Records enabled.
<?xml version="1.0" encoding="UTF-8"?>
<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>work.basil.demo</groupId>
<artifactId>Demo5</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Demo5</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--The following `source` and `target` tags are now replaced by `release` seen further down below.-->
<!--<maven.compiler.source>15</maven.compiler.source>-->
<!--<maven.compiler.target>15</maven.compiler.target>-->
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0-M1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!--Enable Java language preview features. Specify Java version.-->
<!--This `release` tag replaced the pair of `source` and `target` tags seen commented-out near top of this POM.-->
<release>15</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
JavaFX
You mentioned JavaFX in your Question. Note that this discussion applies to any and all Maven-driven Java projects. This includes JavaFX projects as well as Jakarta Servlets, console apps, and so on. Nothing here is specific to JavaFX.
I have a maven project and when I try to execute through pom.xml file, getting the compilation error as follows
C:testscripts/TC_Maintenance.java:[137,48] "strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)"
I have configured jdk 1.8 in maven, could you please resolve this issue.
We have parent pom as well that is called in the pom.xml file
<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>
Please validate compiler option in pom.xml file also validate your maven is using correct java version using mvn -version.
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
more details about in set-compiler-source-and-target
Please make sure that you have added this plugin to your pom.xml. This will ensure that the build task uses version 1.7. We often miss this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Instead of including it as a plug-in, I added the section under properties above Dependencies:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I should add that this is what I believe has already been offered, but I think you added the code in the wrong area of your pom.xml, at least based upon your commented reply (it's hard to read as a comment - no formatting)
By default Maven project takes jre file try to change it to JDK JRE and check whether you are able to resolve this issue.
I have just installed maven on an new ubuntu system, which includes the maven-compiler-plugin. I have a java project that was previously building fine, defaulting to a javac source and target of 5 (jdk 1.5). However, the project is now trying to compile using jdk1.3 on the new system. Is there an easy way to configure the system to use >=jdk5 ?
Here's some of the configuration details of the system:
$ java -version
java version "1.6.0_45"
$ dpkg -s maven
Package: maven
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 1489
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: all
Version: 3.0.4-2
$ dpkg -s libmaven-compiler-plugin-java
Package: libmaven-compiler-plugin-java
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 75
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: all
Source: maven-compiler-plugin
Version: 2.0.2-6
I've checked the maven-compiler-plugin-2.0.2.pom file, and plexus-compiler-javac.originalVersion and others are set to 1.5.3.
I know I can set this on a per-project basis by including a source/target tag in a plugin context, but I'd like to configure maven-compiler to default to jdk5 or higher without having to do this across a large number of projects.
how can i do this?
In your pom specify the following to set the compiler to JDK5:
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
i.e.
<project>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
...
</project>
I specify mine prior to the dependencies, although so long as its part of the project element you should be able to place it anywhere inside the pom.
I ran into a similar issue with Maven previously, this fixed it for me. Essentially what this does is set the -source and -target flags to the value specified and passes it to the compiler. Newer plugins default to 1.5.
In order to use the default approach without specifying the properties, you will need to be running a later version of Maven.
I suppose you could also set up a template via your IDE to include this in all new pom files. Of course the actual implementation would depend upon your IDE...
See The apache maven compiler plugin documentation as well as Setting the source and compiler examples for more details.
I tried the maven-compiler-plugin approach and it proved cumbersome as there are plugins like maven-surefire-plugin and maven-cobertura-plugin which still fail due to incompatibility issues.
The better approach was to use maven-toolchain-plugin.
Step 1
Create /.m2/toolchains.xml
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.6</version>
<vendor>apple</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<!-- other toolchains -->
<!--
<toolchain>
<type>netbeans</type>
<provides>
<version>5.5</version>
</provides>
<configuration>
<installDir>/path/to/netbeans/5.5</installDir>
</configuration>
</toolchain>
-->
Step 2
Add maven-toolchain-plugin to plugins section in your project pom.xml.
*If using maven 3, ensure this goes into pluginManagement as well *
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.7</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
Voila all your other plugins pick up the right JDK. Hope it helps. I spent almost half day on this exact issue today.
Default value for source and target was 1.3 in older versions of maven-compiler plugin (like 2.0.2-6).
Use at least a 3.0 version of the Maven compiler plugin to get this back to the original behaviour, or just configure that plugin to get source and target to appropriate values.
I used following settings to set maven default java compiler version.
first, modify maven settings.xml:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
second, in eclipse preferences, make the java home point to jdk home
Suggestion: Use the latest maven compiler plugin.
In order to change the defaults, you should set source and target.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
More Info: maven-compiler-plugin
The simplest solution to such things is using an up-to-date version of Maven (3.1.1) and in particular create a parent pom.xml file for all your projects where you define the configuration and version of your maven-compiler-plugin via pluginManagement or better all of your plugins.
Based on Chinto's answer I got it running on my windows machine with this:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
<jdkHome>C:\path\to\openJDK1.8.0</jdkHome>
</configuration>
</toolchain>
</toolchains>
I'm trying to generate java classes for OGC KML 2.2 as part of the maven generate-sources process using the org.codehaus.mojo xmlbeans-maven-plugin. The java code appears to be generated correctly, but I get tons of errors during compilation complaining that 'package org.apache.xmlbeans'. XMLBeans is clearly a dependency, it exists in my ~/.m2 repository, and I've been peek in the jar to make sure the classes are there. It looks like XMLBeans is successfully generating java files in target/generated-sources, but somehow its absent from the classpath during compilation.
I've tried changing the scope of the org.apache.xmlbeans dependency, but to no avail.
Here's the pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>net.opengis</groupId>
<artifactId>ogc-kml</artifactId>
<version>2.2.0</version>
<packaging>pom</packaging>
<name>ogc-kml</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<inherited>true</inherited>
<configuration>
<download>true</download>
<schemaDirectory>src/main/xsd</schemaDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>
The project consists of a single src/main/xsd folder containing the two xsds from http://schemas.opengis.net/kml/2.2.0/. The entire folder structure is at https://github.com/iancw/maven-xmlbeans-question.
I can compile the classes by hand if I put the xmlbeans jar from my ~/.m2 repo on the classpath, e.g.
xmlbeans$ javac -classpath ~/.m2/repository/org/apache/xmlbeans/xmlbeans/2.4.0/xmlbeans-2.4.0.jar org/w3/x2005/atom/*.java org/w3/x2005/atom/impl/*.java net/opengis/kml/x22/*.java x0/oasisNamesTcCiqXsdschemaXAL2/*.java x0/oasisNamesTcCiqXsdschemaXAL2/impl/*.java net/opengis/kml/x22/*.java net/opengis/kml/x22/impl/*.java
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
xmlbeans$
I've looked through a number of examples and it seems like I'm doing this right. I haven't seen anyone else complain of this issue. Any maven mavens have suggestions?
(A curious side note is that although i've tried both 2.4.0 and 2.6.0 of the xmlbeans dependency, maven hasn't ever seemed to download the 2.6.0 version into my repository)
From the POM file that you've included in your question you have only defined the xmlbeans dependency in the dependencyManagement section. You also need to define it in your dependencies section of your POM before it will be included in the classpath at build time.
So for example your POM would be:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
...
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
</dependencies>
One Additional issue that may look similar,
Check your java install jdk and ext folders for older beans jar.
The plugin puts the project dependencies at the end of the classpath.
I have a Java-Groovy Eclipse project that I build with Maven. I have added the Maven Groovy plugin to the pom.xml such that I can build/test the Java and Groovy sources on the command-line using Maven.
I would like to have some way to automatically generate the Eclipse .project and .classpath files from my pom.xml. If I run mvn eclipse:eclipse it seems to assume that it's a Java project, so there's no way to (for example) run the tests in src/main/groovy from within Eclipse.
I'm using the STS Eclipse distribution, which includes support for Groovy/Grails. All I'm missing is a way to automatically create the appropriate .classpath and .project files.
Thanks!
P.S. I know IntelliJ is better, but I don't have a license
Here is configuration I found that works when Java calls Groovy code
and when Groovy calls Java code fitting good within groovy eclipse IDE plugin (nature).
There is no need for additional source folders for groovy. It just works!
Using:
mvn clean install eclipse:clean eclipse:eclipse
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>true</verbose>
<extensions>true</extensions>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
</additionalProjectnatures>
<sourceIncludes>
<sourceInclude>**/*.groovy</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
</plugins>
</build>
You should try the Groovy-Eclipse m2eclipse integration. It is available here:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.6/
With this installed, your maven projects will be automatically configured as groovy-eclipse projects when you import them into your workspace.
If you would like to create a Groovy project just by calling mvn eclipse:eclipse you have to configure your project. As follows a snippet how you configure your maven eclipse plugin so that your project becomes a Groovy project in Eclipse. That snippet must go into your projects pom.xml by the way.
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
</additionalProjectnatures>
<sourceIncludes>
<sourceIncludes>**/*.groovy</sourceIncludes>
</sourceIncludes>
</configuration>
</plugin>
</plugins>
</build>
...
When you now call mvn eclipse:eclipse maven creates the .project and .classpath files. .project contains the new project nature what makes it a Groovy project and .classpath contains the */*.groovy* what makes Eclipse treating any file that ends on .groovy as a source file.
Please see also http://maven.apache.org/plugins/maven-eclipse-plugin/examples/provide-project-natures-and-build-commands.html
There is another best way to create maven groovy project. Please follow below steps:
Navigate to https://start.spring.io/ from your browser.
Select project as maven and language as groovy as shown below.
Select other options as per your build requirement like packaging, java version and project name.
Now click on Generate radio button at the bottom and a maven groovy project will be downloaded.
Open Eclipse and import the downloaded maven project and it's ready to use for your groovy scripting with maven integration.