Search code examples
javaeclipsemavenm2eclipsests-springsourcetoolsuite

Why won't STS / m2eclipse track my Maven dependencies?


I use SpringSource Tool Suite (STS) with Maven + m2eclipse. On my latest project, I am facing an issue importing an existing Maven project correctly from SVN into STS. When I use import -> Maven -> 'existing Maven project', the project will import but will have the following issues:

  • The src/main/java and src/test/java are not picked up as source folders. STS will configure src as the source folder and main/test are added to the package names.
  • The Maven dependencies library is not added to the java build path.

I can manually correct the source folders, but when I try to add the 'Maven managed dependencies' library to the build path I get the message 'Use Maven Project Settings to configure Maven dependency resolution' and the library is not added. The only Maven project settings I seem to be able to set are the active profile and 'resolve dependencies from workspace projects' (checked), neither of which seem to make any difference.

Add Maven Managed Dependencies Maven Project Settings

If I run mvn install from the command line the project builds successfully. If I run mvn eclipse:eclipse and then import into STS, then everything works as expected, but then of course you have to re-run this every time you update the pom, which is undesirable.

I worked around it by running mvn eclipse:eclipse and then manually updating .classpath to eliminate the M2_REPO dependencies added by eclipse:eclipse and adding the m2eclipse dependency entry:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
  <attributes>
    <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
  </attributes>
</classpathentry>

Then I imported existing Maven project and it worked as expected. However, this is a hack and I'm not sure what other consequences running eclipse:ecplise has when working with m2eclipse.

I have worked on other maven projects and I faced no issues importing them correctly.

Possibly relevant information:

  • This is a webapp project.
  • The subversion repo contains only the pom.xml and the src folder.
  • I use an external maven installation and it is version 3.0.3
  • We use an onsite Artifactory repo for artifact download

Things I have tried:

  • Download from SVN to local disk then import to STS using import existing Maven project
  • Import project into STS from SVN, configuration -> enable Maven nature
  • Run mvn eclipse:eclipse then import (works but requires manual classpath edits for m2e)
  • Search stackoverflow, found this question which is very similar but the answers do not seem to solve my issue.

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>com.company.group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>My Project</name>
    <description>My Project</description>

    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
        <!-- Lots of other versions omitted -->
    </properties>

    <repositories>
        <repository>
            <id>repoId</id>
            <name>repoName</name>
            <url>http://fake.company.com/artifactory/repo</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>repoId</id>
            <name>repoName</name>
            <url>http://fake.company.com/artifactory/repo</url>
        </pluginRepository>
    </pluginRepositories>

    <!-- Configurations required for deploy plugin. Artifacts are deployed to 
        artifactory -->
    <distributionManagement>
        <repository>
            <id>repoId</id>
            <name>repoName-releases</name>
            <url>http://fake.company.com/artifactory/apps-releases-local</url>
        </repository>
        <snapshotRepository>
            <id>repoId</id>
            <name>repoName-snapshots</name>
            <url>http://fake.company.com/artifactory/apps-snapshots-local</url>
        </snapshotRepository>
    </distributionManagement>

    <scm>
        <connection>scm:svn:https://fake.company.com/svn/fake-repo/trunk</connection>
        <developerConnection>scm:svn:https://fake.company.com/svn/fake-repo/trunk</developerConnection>
        <url>https://fake.company.com/svn/fake-repo/trunk</url>
    </scm>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <!-- Lots of other dependencies omitted -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/TestUtil.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <compilerVersion>${java-version}</compilerVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>war-name</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <formats>
                        <format>html</format>
                    </formats>
                    <instrumentation>
                        <ignores>
                            <ignore>path/**/*Test.class</ignore>
                        </ignores>
                        <excludes>
                            <exclude>path/Constants.class</exclude>
                            <exclude>path/*.class</exclude>
                        </excludes>
                    </instrumentation>
                    <check>
                        <haltOnFailure>false</haltOnFailure>
                        <totalBranchRate>25</totalBranchRate>
                        <totalLineRate>41</totalLineRate>
                        <packageLineRate>25</packageLineRate>
                        <packageBranchRate>15</packageBranchRate>
                    </check>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>cobertura</goal>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${org.apache.cxf-version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/wsdl/automation.wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated/cxf</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                        <projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                </configuration>
            </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.cxf</groupId>
                                        <artifactId>
                                            cxf-codegen-plugin
                                        </artifactId>
                                        <versionRange>
                                            [${org.apache.cxf-version},)
                                        </versionRange>
                                        <goals>
                                            <goal>wsdl2java</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-compiler-plugin</artifactId>
                                        <versionRange>[2.3.2,)</versionRange>
                                        <goals>
                                            <goal>compile</goal>
                                            <goal>testCompile</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <profiles>
        <!-- Deployment profiles omitted -->
    </profiles>
</project>

Does anyone have any ideas on how to:

  1. get the m2eclipse import to work correctly? OR
  2. configure STS to allow me to add maven managed dependecies to the build path after project creation / conversion?

Solution

  • The following section:

                               <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-compiler-plugin</artifactId>
                                        <versionRange>[2.3.2,)</versionRange>
                                        <goals>
                                            <goal>compile</goal>
                                            <goal>testCompile</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
    

    has the unfortunate consequence of disabling the java compiler in your build. Remove it and I would imagine that things work.

    I also see this:

                       <projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
    

    Is this a groovy project? If so are you using gmaven or groovy-eclipse-compiler?