Maven为另一个项目的测试建立战争罐子

时间:2014-07-18 08:07:29

标签: java maven jar

PROJECT-A是一场战争。它有一些我想在PROJECT-B的单元测试中使用的代码。

要做到这一点,我想我需要PROJECT-A来输出一个带有它的类的jar。以下是POM的摘录:

<project
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <parent>
      <groupId>au.com.name.redacted</groupId>
      <artifactId>PARENT-PROJECT</artifactId>
      <version>1.0</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <artifactId>PROJECT-A</artifactId>
   <packaging>war</packaging>

   ... snip

   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.4</version>
         <executions>
            <execution>
               <id>PROJECT-A-jar</id>
               <phase>package</phase>
               <goals>
                  <goal>jar</goal>
               </goals>
            </execution>
         </executions>
      </plugin>

当我跑步时

 cd PROJECT-A
 mvn clean install

输出

[INFO] --- maven-jar-plugin:2.4:jar (PROJECT-A-jar) @ PROJECT-A ---
[INFO] Building jar: D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ PROJECT-A ---
[INFO] Installing D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar to C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war

我确实看到了

D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.war

但我没有在我的回购中看到罐子 - 只有战争。

C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war

那么在PROJECT-B中我试图让它得到PROJECT-A作为依赖..

<project
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <parent>
      <groupId>au.com.name.redacted</groupId>
      <artifactId>PARENT-PROJECT</artifactId>
      <version>1.0</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <artifactId>PROJECT-B</artifactId>
   <packaging>war</packaging>
... snip
<dependency>
   <groupId>au.com.name.redacted</groupId>
   <artifactId>PROJECT-A</artifactId>
   <version>1.0</version>
   <scope>test</scope>
</dependency>

它失败了

     [ERROR] Failed to execute goal on project PROJECT-B: Could not resolve dependencies for project au.com.name.redacted:PROJECT-B:war:1.0: Failure to find au.com.name.redacted:PROJECT-A:jar:1.0 in http://repo.server.com.au:8081/artifactory/repo was cached in the local repository, resolution will not be reattempted until the update interval of repo has elapsed or updates are forced -> [Help 1]

1 个答案:

答案 0 :(得分:1)

我为此目的使用了war插件。也许这会有所帮助:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>

它会在构建之后生成战争和jar来源,它将被提交到本地m2 repo。配置<attachClasses>true</attachClasses>很重要。

在项目B的pom中使用类似的东西(重要部分 - <classifier>classes</classifier>):

<dependency>
    <groupId>projectA-groupid</groupId>
    <artifactId>projectA-artifactId</artifactId>
    <version>projectA-version</version>
    <classifier>classes</classifier>
</dependency>
相关问题