Eclipse + Maven + Groovy: src/test/groovy director

2019-03-30 09:24发布

问题:

I'm developing a Java web project in Eclipse (STS version 2.8.1.RELEASE) with Maven (version 2.2.1) and unit tests written in Groovy. The unit tests are located under src/test/groovy. Furthermore I'm using the m2eclipse plugin for Eclipse (version 1.0) and the Gmaven plugin in Maven (version 1.3).

Building in Maven works without problems: the groovy files are compiled and executed as tests. For the unit tests to work in Eclipse I added the Groovy nature to the project, added the folder src/test/groovy under Configure Build Path... and set the output folder to target/test-classes.

This works until I do an update of the project configuration under Maven -> Update Project Configuration.... After I do this every time the directory src/test/groovy gets removed from the source folders in Eclipse and I have to add it again and set the output directory.

Is there something I am missing or why is Eclipse deleting my source folder configuration every time I do an update of the project configuration?

My GMaven configuration looks as follows:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <configuration>
        <providerSelection>1.7</providerSelection>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

回答1:

I had a similar issue, mine was that eclipse was preventing me from writing groovy files into java folder. But you could try the same configuration out, or check out my whole pom at github

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
                </additionalProjectnatures>
                <!-- Source includes is necessary to allow groovy files in the java 
                    folder, else eclipse will throw a filtering exception -->
                <sourceIncludes>
                    <sourceInclude>**/*.groovy</sourceInclude>
                </sourceIncludes>
                <!-- Download sources will make maven download and attach source files 
                    where available -->
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>

After I put in this configuration in the pom, the .classpath got generated properly.



回答2:

Using the builder-helper-maven-plugin helped. Eclipse adds the source folder and sets the output folder correctly. I used the following configuration:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>


回答3:

Check out this issue on SoF here. You need to add the build-helper-maven-plugin to get the resources added.



回答4:

You need to install the groovy-eclipse configurator for m2eclipse. It is available from this update site:

http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.7/

If you are using m2eclipse v1.0 or later, then choose to install from here:

m2e Configurator for Groovy-Eclipse (Optional)  

If you are using an older version of m2eclipse, then install from here:

Groovy-Eclipse m2eclipse integration pre v1.0 (deprecated)


回答5:

Try by adding your Groovy source directory.