在“测试”执行阶段排除“提供的”范围依赖关系?

时间:2013-06-09 05:19:20

标签: maven maven-3

我在测试执行期间在类路径中看到一个带有provided范围的jar。鉴于provided范围的定义,我不希望jar在任何执行阶段出现在类路径中。

我已使用M2E“Dependency Hierarchy”视图确认范围是正确的。我已经尝试使用classpathDependencyScopeExclude中的maven-surefire-plugin选项来阻止依赖项显示,但它没有奏效:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14.1</version>
        <configuration>
          <!--
            We always want to exclude provided deps. I'm not sure why this
            isn't the default.
          -->
          <classpathDependencyScopeExclude>provided</classpathDependencyScopeExclude>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

以下是相关的依赖关系定义:

父POM

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>jsr311-api</artifactId>
  <version>1.1.1</version>
</dependency>

模块POM

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>jsr311-api</artifactId>
  <scope>provided</scope>
</dependency>

罐子在那里是正常的吗?我该怎么办才能防止它出现?

编辑:我在父POM上运行mvn dependency:tree。这是相关项目的输出:

[INFO] ------------------------------------------------------------------------
[INFO] Building sibyl.transport.impl.http.server 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ sibyl.transport.impl.http.server ---
[INFO] com.sigpwned.analytics:sibyl.transport.impl.http.server:jar:0.0.1-SNAPSHOT
[INFO] +- com.sigpwned.analytics:sibyl.transport:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.sigpwned.analytics:commons:jar:0.0.1-SNAPSHOT:compile
[INFO] +- javax.ws.rs:jsr311-api:jar:1.1.1:provided
[INFO] +- com.sun.jersey:jersey-server:jar:1.17.1:test (scope not updated to compile)
[INFO] |  +- asm:asm:jar:3.1:test
[INFO] |  \- com.sun.jersey:jersey-core:jar:1.17.1:test
[INFO] +- com.sun.jersey:jersey-servlet:jar:1.17.1:compile
[INFO] +- org.eclipse.jetty:jetty-servlet:jar:9.0.3.v20130506:test
[INFO] |  \- org.eclipse.jetty:jetty-security:jar:9.0.3.v20130506:test
[INFO] |     \- org.eclipse.jetty:jetty-server:jar:9.0.3.v20130506:test
[INFO] |        +- org.eclipse.jetty.orbit:javax.servlet:jar:3.0.0.v201112011016:test
[INFO] |        +- org.eclipse.jetty:jetty-http:jar:9.0.3.v20130506:test
[INFO] |        |  \- org.eclipse.jetty:jetty-util:jar:9.0.3.v20130506:test
[INFO] |        \- org.eclipse.jetty:jetty-io:jar:9.0.3.v20130506:test
[INFO] +- com.sigpwned.analytics:sibyl.transport.impl.http.client:jar:0.0.1-SNAPSHOT:test
[INFO] |  \- org.apache.httpcomponents:httpclient:jar:4.2.5:test
[INFO] |     +- org.apache.httpcomponents:httpcore:jar:4.2.4:test
[INFO] |     +- commons-logging:commons-logging:jar:1.1.1:test
[INFO] |     \- commons-codec:commons-codec:jar:1.6:test
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

同样,提供范围的jar我希望不会出现在类路径上,但确实是JAX-RS规范API,如上所示为javax.ws.rs:jsr311-api:jar:1.1.1:provided。我相信这证实了这种依赖性只出现在provided范围内。

编辑:如果我运行测试,这是运行的命令行(来自ps):

sigpwned       11771   0.0  1.2  4019788 100240   ??  S    10:58PM   0:05.92 /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:59974 -Dfile.encoding=UTF-8 -classpath /Users/aboothe/Documents/workspaces/w2oanalytics/sibyl.transport.impl.http.server/target/test-classes:/Users/aboothe/Documents/workspaces/w2oanalytics/sibyl.transport.impl.http.server/target/classes:/Users/aboothe/Documents/workspaces/w2oanalytics/sibyl/sibyl.transport/target/classes:/Users/aboothe/Documents/workspaces/w2oanalytics/commons/target/classes:/Users/aboothe/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar:/Users/aboothe/.m2/repository/com/sun/jersey/jersey-server/1.17.1/jersey-server-1.17.1.jar:/Users/aboothe/.m2/repository/asm/asm/3.1/asm-3.1.jar:/Users/aboothe/.m2/repository/com/sun/jersey/jersey-core/1.17.1/jersey-core-1.17.1.jar:/Users/aboothe/.m2/repository/com/sun/jersey/jersey-servlet/1.17.1/jersey-servlet-1.17.1.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-servlet/9.0.3.v20130506/jetty-servlet-9.0.3.v20130506.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-security/9.0.3.v20130506/jetty-security-9.0.3.v20130506.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-server/9.0.3.v20130506/jetty-server-9.0.3.v20130506.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-http/9.0.3.v20130506/jetty-http-9.0.3.v20130506.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-util/9.0.3.v20130506/jetty-util-9.0.3.v20130506.jar:/Users/aboothe/.m2/repository/org/eclipse/jetty/jetty-io/9.0.3.v20130506/jetty-io-9.0.3.v20130506.jar:/Users/aboothe/Documents/workspaces/w2oanalytics/sibyl.transport.impl.http.client/target/classes:/Users/aboothe/.m2/repository/org/apache/httpcomponents/httpclient/4.2.5/httpclient-4.2.5.jar:/Users/aboothe/.m2/repository/org/apache/httpcomponents/httpcore/4.2.4/httpcore-4.2.4.jar:/Users/aboothe/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/Users/aboothe/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/Users/aboothe/.m2/repository/junit/junit/4.11/junit-4.11.jar:/Users/aboothe/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/aboothe/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/350/1/.cp/:/Users/aboothe/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/349/1/.cp/ org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 59973 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames com.sigpwned.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests

如果你拉出类路径,这就是它的样子:

/Users/sigpwned/Documents/workspaces/siganalytics/sibyl.transport.impl.http.server/target/test-classes
/Users/sigpwned/Documents/workspaces/siganalytics/sibyl.transport.impl.http.server/target/classes
/Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport/target/classes
/Users/sigpwned/Documents/workspaces/siganalytics/commons/target/classes
/Users/sigpwned/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar
/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-server/1.17.1/jersey-server-1.17.1.jar
/Users/sigpwned/.m2/repository/asm/asm/3.1/asm-3.1.jar
/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-core/1.17.1/jersey-core-1.17.1.jar
/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-servlet/1.17.1/jersey-servlet-1.17.1.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-servlet/9.0.3.v20130506/jetty-servlet-9.0.3.v20130506.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-security/9.0.3.v20130506/jetty-security-9.0.3.v20130506.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-server/9.0.3.v20130506/jetty-server-9.0.3.v20130506.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-http/9.0.3.v20130506/jetty-http-9.0.3.v20130506.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-util/9.0.3.v20130506/jetty-util-9.0.3.v20130506.jar
/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-io/9.0.3.v20130506/jetty-io-9.0.3.v20130506.jar
/Users/sigpwned/Documents/workspaces/siganalytics/sibyl.transport.impl.http.client/target/classes
/Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpclient/4.2.5/httpclient-4.2.5.jar
/Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpcore/4.2.4/httpcore-4.2.4.jar
/Users/sigpwned/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
/Users/sigpwned/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
/Users/sigpwned/.m2/repository/junit/junit/4.11/junit-4.11.jar
/Users/sigpwned/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/Users/sigpwned/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/350/1/.cp/
/Users/sigpwned/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/349/1/.cp/

编辑:在调查classpathDependencyScopeExclude文档后,我确定您只能排除compileruntimetest依赖项,而不是provided。这解释了为什么该功能不起作用。在尝试下面的@hello建议之后,我确定classpathDependencyExclude在直接使用maven运行时有效。这是我从surefire jar的MANIFEST中删除的类路径:

  file:/Users/sigpwned/.m2/repository/org/apache/maven/surefire/surefire-booter/2.14.1/surefire-booter-2.14.1.jar
  file:/Users/sigpwned/.m2/repository/org/apache/maven/surefire/surefire-api/2.14.1/surefire-api-2.14.1.jar
  file:/Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport.impl.http.server/target/test-classes/
  file:/Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport.impl.http.server/target/classes/
  file:/Users/sigpwned/.m2/repository/com/siggroup/analytics/sibyl.transport/0.0.1-SNAPSHOT/sibyl.transport-0.0.1-SNAPSHOT.jar
  file:/Users/sigpwned/.m2/repository/com/siggroup/analytics/commons/0.0.1-SNAPSHOT/commons-0.0.1-SNAPSHOT.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-server/9.0.3.v20130506/jetty-server-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-http/9.0.3.v20130506/jetty-http-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-util/9.0.3.v20130506/jetty-util-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-io/9.0.3.v20130506/jetty-io-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-servlet/9.0.3.v20130506/jetty-servlet-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-security/9.0.3.v20130506/jetty-security-9.0.3.v20130506.jar
  file:/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-server/1.17.1/jersey-server-1.17.1.jar
  file:/Users/sigpwned/.m2/repository/asm/asm/3.1/asm-3.1.jar
  file:/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-core/1.17.1/jersey-core-1.17.1.jar
  file:/Users/sigpwned/.m2/repository/com/sun/jersey/jersey-servlet/1.17.1/jersey-servlet-1.17.1.jar
  file:/Users/sigpwned/.m2/repository/com/siggroup/analytics/sibyl.transport.impl.http.client/0.0.1-SNAPSHOT/sibyl.transport.impl.http.client-0.0.1-SNAPSHOT.jar
  file:/Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpclient/4.2.4/httpclient-4.2.4.jar
  file:/Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpcore/4.2.4/httpcore-4.2.4.jar
  file:/Users/sigpwned/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
  file:/Users/sigpwned/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
  file:/Users/sigpwned/.m2/repository/com/siggroup/analytics/sibyl.transport/0.0.1-SNAPSHOT/sibyl.transport-0.0.1-SNAPSHOT-tests.jar
  file:/Users/sigpwned/.m2/repository/junit/junit/4.11/junit-4.11.jar
  file:/Users/sigpwned/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar

请注意,jsr311-api.jar不存在。成功!这是有效的配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire-plugin.version}</version>
  <configuration>
    <!--
      We never want to include these API deps on an execution classpath.
      I would think this is how the "provided" scope works - just
      compile against the jar, and then I'll bring it myself at
      execution time - but provided is only excluded during the runtime
      phase, not the testing phase. So, we have to exclude all our
      API JARs by hand.
    -->
    <classpathDependencyExcludes>
      <classpathDependencyExclude>javax.ws.rs:jsr311-api</classpathDependencyExclude>
    </classpathDependencyExcludes>
    <includes>
      <include>**/*Tests.*</include>
    </includes>
  </configuration>
</plugin>

但是,此解决方案 似乎无法在M2E上运行。这是我在使用M2E执行测试期间从ps中提取的类路径:

  /Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport.impl.http.server/target/test-classes
  /Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport.impl.http.server/target/classes
  /Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport/target/classes
  /Users/sigpwned/Documents/workspaces/siganalytics/commons/target/classes
> /Users/sigpwned/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-server/9.0.3.v20130506/jetty-server-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-http/9.0.3.v20130506/jetty-http-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-util/9.0.3.v20130506/jetty-util-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-io/9.0.3.v20130506/jetty-io-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-servlet/9.0.3.v20130506/jetty-servlet-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/org/eclipse/jetty/jetty-security/9.0.3.v20130506/jetty-security-9.0.3.v20130506.jar
  /Users/sigpwned/.m2/repository/com/sun/jersey/jersey-server/1.17.1/jersey-server-1.17.1.jar
  /Users/sigpwned/.m2/repository/asm/asm/3.1/asm-3.1.jar
  /Users/sigpwned/.m2/repository/com/sun/jersey/jersey-core/1.17.1/jersey-core-1.17.1.jar
  /Users/sigpwned/.m2/repository/com/sun/jersey/jersey-servlet/1.17.1/jersey-servlet-1.17.1.jar
  /Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport.impl.http.client/target/classes
  /Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpclient/4.2.4/httpclient-4.2.4.jar
  /Users/sigpwned/.m2/repository/org/apache/httpcomponents/httpcore/4.2.4/httpcore-4.2.4.jar
  /Users/sigpwned/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
  /Users/sigpwned/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
  /Users/sigpwned/Documents/workspaces/siganalytics/sibyl/sibyl.transport/target/test-classes
  /Users/sigpwned/.m2/repository/junit/junit/4.11/junit-4.11.jar
  /Users/sigpwned/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
  /Users/sigpwned/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/350/1/.cp/
  /Users/sigpwned/Downloads/eclipse/configuration/org.eclipse.osgi/bundles/349/1/.cp/

不幸的是,jsr311-api.jar仍然存在。这让我相信这个问题得到了解答(答案是“使用classpathDependencyExcludes”),但M2E中存在一个错误。我在Eclipse中运行了“检查更新”并且没有找到M2E的更新,所以我应该运行最新的稳定版本。我将在M2E上提交一个错误并在此处更新问题,以便找到此问题的任何人都可以跟踪。

编辑:此问题与M2E already raised有关,答案似乎是“此时Surefire集成超出了M2E的范围。”不鼓励。 :/

1 个答案:

答案 0 :(得分:7)

  

鉴于provided范围的定义,我不希望jar在任何执行阶段出现在类路径上。

这不是provided范围的行为;见Dependency Scopeprovided范围与compile一样,适用于编译和测试类所必需的类。如果他们在test期间失踪,那么您将获得NoClassDefFoundError

在打包和部署期间出现差异,假设您的目标环境(例如,servlet容器)将提供其自己的相同类的副本,则war中省略了这些工件。