繁体   English   中英

Maven为另一个项目的测试而战

[英]Maven building jar of a war for another project's tests

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]

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

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

生成后,它将使用源生成战争和震撼,并将其提交给本地m2回购。 配置<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>

暂无
暂无

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

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