简体   繁体   English

为什么STS / m2eclipse无法跟踪我的Maven依赖关系?

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

I use SpringSource Tool Suite (STS) with Maven + m2eclipse. 我将SpringSource Tool Suite(STS)与Maven + m2eclipse结合使用。 On my latest project, I am facing an issue importing an existing Maven project correctly from SVN into STS. 在我的最新项目中,我遇到了将现有Maven项目从SVN正确导入STS的问题。 When I use import -> Maven -> 'existing Maven project' , the project will import but will have the following issues: 当我使用import -> Maven -> 'existing Maven project' ,该项目将导入,但是会出现以下问题:

  • The src/main/java and src/test/java are not picked up as source folders. 不会将src/main/javasrc/test/java选作源文件夹。 STS will configure src as the source folder and main/test are added to the package names. STS将src配置为源文件夹,并将main / test添加到程序包名称中。
  • The Maven dependencies library is not added to the java build path. Maven依赖库未添加到Java构建路径。

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. 我可以手动更正源文件夹,但是当我尝试将“ Maven管理的依赖项”库添加到构建路径时,我收到消息“使用Maven项目设置配置Maven依赖项解析”,并且未添加该库。 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. 我似乎只能设置的Maven项目设置是活动配置文件和“解决工作区项目的依赖关系”(已选中),两者似乎都没有任何区别。

添加Maven托管依赖项Maven项目设置

If I run mvn install from the command line the project builds successfully. 如果我从命令行运行mvn install ,则项目将成功构建。 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. 如果我运行mvn eclipse:eclipse并导入到STS中,那么一切都会按预期进行,但是当然,每次更新pom时都必须重新运行它,这是不可取的。

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: 我通过运行mvn eclipse:eclipse eclipse来解决此M2_REPO ,然后手动更新.classpath以消除由eclipse:eclipse添加的M2_REPO依赖关系,并添加m2eclipse依赖项:

<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. 然后,我导入了现有的Maven项目,该项目按预期工作。 However, this is a hack and I'm not sure what other consequences running eclipse:ecplise has when working with m2eclipse. 但是,这是一个hack,我不确定在使用m2eclipse时运行eclipse:ecplise有什么其他后果。

I have worked on other maven projects and I faced no issues importing them correctly. 我从事过其他Maven项目,并且正确导入它们没有任何问题。

Possibly relevant information: 可能相关的信息:

  • This is a webapp project. 这是一个webapp项目。
  • The subversion repo contains only the pom.xml and the src folder. 子版本库仅包含pom.xmlsrc文件夹。
  • I use an external maven installation and it is version 3.0.3 我使用外部Maven安装,它是3.0.3版
  • We use an onsite Artifactory repo for artifact download 我们使用现场Artifactory仓库进行人工制品下载

Things I have tried: 我尝试过的事情:

  • Download from SVN to local disk then import to STS using import existing Maven project 从SVN下载到本地磁盘,然后使用导入现有的Maven项目导入到STS
  • Import project into STS from SVN, configuration -> enable Maven nature 从SVN将项目导入STS,配置->启用Maven性质
  • Run mvn eclipse:eclipse then import (works but requires manual classpath edits for m2e) 运行mvn eclipse:eclipse然后导入(可以,但是需要手动编辑m2e的类路径)
  • Search stackoverflow, found this question which is very similar but the answers do not seem to solve my issue. 搜索stackoverflow,发现这个问题非常相似,但是答案似乎无法解决我的问题。

pom.xml: 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? 使m2eclipse导入正常工作? OR 要么
  2. configure STS to allow me to add maven managed dependecies to the build path after project creation / conversion? 配置STS以允许我在项目创建/转换后将Maven管理的依赖项添加到构建路径?

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. 不幸的结果是在构建中禁用了Java编译器。 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? 如果是这样,您使用的是gmaven还是groovy-eclipse-compiler?

I've had this issue a few times now and each time the solution is in the spirit of Andrew's answer: there is some section of the pom that maven accepts but m2eclipse barfs on. 我现在已经遇到过几次这个问题,每次解决方案都是按照安德鲁的回答的精神进行的:maven可以接受pom的某些部分,但是m2eclipse可以使用barfs。

So I recommend removing parts of the pom 1 by 1 until you can mavenize the project successfully. 因此,我建议将pom的部分逐1删除,直到可以成功完成项目。 Just keep running maven -> update configuration after each pom edit until it works as it should. 每次pom编辑后,只要继续运行maven -> update configuration ,直到它可以正常工作即可。 I usually start by removing the plugin configuration tag blocks one at a time, starting with the most suspicious (ie most complicated). 我通常从一次删除插件配置标签块开始,从最可疑(即最复杂)开始。

Once it mavenizes, you can revert the pom and it should still work as expected. 一旦变通了,您可以还原pom,它仍然可以按预期工作。

After I get running, I'd research the offending configuration(s) to try to figure out what the 'proper' fix is (according to m2eclipse, anyway). 运行后,我将研究有问题的配置,以试图弄清楚“正确”的解决方案是什么(无论如何,根据m2eclipse)。

The workaround I posted in my question will work temporarily, and I never discovered any negative side effects, but this solution feels less hacky and will help you isolate and solve the issue permanently. 我在问题中发布的解决方法将暂时起作用,并且我从未发现任何负面影响,但是该解决方案的安全性较低,可以帮助您永久隔离并解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM