简体   繁体   中英

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

PROJECT-A is a war. It has some code I want to use in the unit tests of PROJECT-B.

To do this, I think I need PROJECT-A to output a jar with it's classes. Here is an excerpt of the 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>

When I run

 cd PROJECT-A
 mvn clean install

it outputs

[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

I do see

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

but I do not see the jar in in my repo - only the war.

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

So then in PROJECT-B I am attempting to have it get PROJECT-A as a dependency..

<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>

And it fails with

     [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]

I've used war plugin for this purpose. Maybe this will help:

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

It generates war and jar with sources after build which will be commited to local m2 repo. Configuration <attachClasses>true</attachClasses> is important.

In pom of project B use something like this (important part - <classifier>classes</classifier> ):

<dependency>
    <groupId>projectA-groupid</groupId>
    <artifactId>projectA-artifactId</artifactId>
    <version>projectA-version</version>
    <classifier>classes</classifier>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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