简体   繁体   中英

maven 3 interproject depedency with war packaging

I have Eclipse Indigo and M2E plugin installed.

So essentially I have a standard maven web project (let's call it proj-service) that is built into a war file in the package phase. This all works fine. My issue comes in when I have my other project (lets call it proj1) that needs to use classes from proj-service. I know that this is possible in maven+eclipse but it does not seem to be working at the moment. I have the following in proj1's pom right now:

        <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.mycompany.foo</groupId>
    <artifactId>proj1</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>proj1</name>
    <properties>
        <spring.version>3.1.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!-- Maven Repo Libraries -->
        .........
        <!-- Interproject dependencies -->
        <dependency>
            <groupId>com.mycompany.foo</groupId>
            <artifactId>proj-service</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
    </dependencies>
    <build>
        <finalName>lsoap</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>          
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
    </build>
</project>

Unfortunately with Maven's war packaging you can't reuse classes from war project, because there is no direct build artifact you can use for the class path.

So, in order to do share classes properly you need to extract those common classes into a 3rd common project (jar packaging) and make it as dependency in both of your other projects.

First you have to change the configuration of your proj-service project in the way to change the configuration of the maven-war-plugin:

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

This will it make possible to use the classes from the proj-service project in other projects via the following dependencies:

<dependency>
  <groupId>myGroup</groupId>
  <artifactId>myArtifact</artifactId>
  <version>myVersion</myVersion>
  <classifier>classes</classifier>
</dependency>

This will result in changing your dependency from:

<dependency>
    <groupId>com.mycompany.foo</groupId>
    <artifactId>proj-service</artifactId>
    <version>1.0</version>
    <type>war</type>
</dependency>

into:

<dependency>
    <groupId>com.mycompany.foo</groupId>
    <artifactId>proj-service</artifactId>
    <version>1.0</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