2012-12-26 38 views
0

我正在使用Maven和JUnit构建一个新项目。在maven编译并运行测试后,我使用t7插件在tomcat下运行应用程序。在我想在JUnit测试中设置javaURLContextFactory之前,我没有任何问题。 javaURLContextFactory位于t7插件中,但不在项目的任何依赖项中。由于只将插件定义为插件,JUnit测试失败,因为它无法找到javaURLContextFactory类。如果仅将插件作为依赖项添加,JUnit测试将工作,然后在我想运行或调试时找不到该插件。如果我在两者中定义它,我会得到与解析web.xml有关的奇怪错误。如何使用maven包作为依赖项和插件

这里是我当前的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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.star2star</groupId> 
    <artifactId>distribute</artifactId> 
    <packaging>war</packaging> 
    <version>0.8.1-SNAPSHOT</version> 
    <name>distribute Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.0.1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>javax.servlet.jsp-api</artifactId> 
      <version>2.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>com.googlecode.json-simple</groupId> 
      <artifactId>json-simple</artifactId> 
      <version>1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.mail</groupId> 
      <artifactId>smtp</artifactId> 
      <version>1.4.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpclient</artifactId> 
      <version>4.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-classic</artifactId> 
      <version>1.0.9</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.22</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.12.4</version> 
      <type>maven-plugin</type> 
     </dependency> 
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>14.0-rc1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-email</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tomcat</groupId> 
      <artifactId>tomcat-api</artifactId> 
      <version>7.0.19</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>distribute-v1</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.0</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>com.googlecode.t7mp</groupId> 
       <artifactId>maven-t7-plugin</artifactId> 
       <version>0.9.10.M8</version> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

有什么办法来引用插件类的依赖或引用的依赖作为一个插件?有没有其他方法来解决这个问题?

+0

永远不要使用Maven插件作为依赖项,因为它应该放置在构建区域,但在这种情况下更好[pluginManagement](http://maven.apache.org/pom.html#Plugin_Management)部分。你是否通过** mvn clean package **在命令行测试了你的构建? – khmarbaise

+0

@khmarbaise我注意到插件作为依赖是一件坏事。 pluginManagement很有趣,但我不明白它是如何应用于单个pom.xml的。在执行mvn clean包时遇到了同样的问题,它找不到javaURLContextFactory。 – ahillman3

回答

0

与同事交谈后找到解决方案。

通过包括插件的依赖,我知道这是不好的,并且范围设置为提供,它可用于JUnit测试,但是当它用的是T7插件部署不包括战争。然后单元测试能够运行,使用javaURLContextFactory并且它仍然正确地部署。

相关问题