Maven多模块项目导入模块

时间:2016-02-29 13:10:30

标签: maven inheritance module dependencies project

我在过去3天阅读了很多教程但没有成功。

我有一个maven多模块项目。在父pom中我定义了我的子模块:

  ...
  <groupId>test</groupId>
  <artifactId>parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>parent</name>
  <packaging>pom</packaging>
  <modules>
   <module>common</module>
   <module>backend</module>
  </modules>

公共模块保留公共数据。基本上它提供了谷歌protobuf类,将在我的后端(以及后来的客户端)项目中使用。 所以,我的protobufs正在生成,现在我想在我的后端项目中使用它们。

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>test</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>common</artifactId>
  <name>common</name>
  <packaging>jar</packaging>
  <dependencies>
   <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>2.6.1</version>
   </dependency>
  </dependencies>
  <build>
  <plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.0</version>
        <configuration>
          <protocExecutable>protoc</protocExecutable>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
  </plugins>
  </build>
</project>

pom.xml后端:在依赖项部分中,我定义了要包含的公共项目。

    <?xml version="1.0"?>
    <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">     
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>test</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>backend</artifactId>
    <name>backend</name>
    <packaging>jar</packaging>
    <properties>
    <jersey.version>2.22.2</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>test</groupId>
        <artifactId>common</artifactId>
        <version>${project.parent.version}</version>
      </dependency>
      <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>${jersey.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
   </dependencyManagement>
   <dependencies>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <inherited>true</inherited>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>backend.Main</mainClass>
        </configuration>
      </plugin>
      <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>jersey-service-${project.version}</finalName>
                    <archive>
                        <manifest>                           <mainClass>de.unidue.inf.is.websail.wsbackend.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals><goal>copy-dependencies</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
      </build>
    </project>

现在,我在父项目上使用mvn install来生成jar并将其复制到我的本地存储库。 (我也尝试先在公共项目上调用命令)。一切都很好,但我不能在后端模块中使用我的protobufs和其他类..

后端模块的依赖关系树甚至没有显示我的“test.common”依赖关系。这就是为什么我不能包括这些课程。它非常令人沮丧!我尝试了很多,例如,在我的普通pom中使用maven编译器插件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals><goal>copy-dependencies</goal></goals>
            </execution>
        </executions>
    </plugin>

但它仍然不起作用。我想念什么吗?在查看我的本地存储库时,我可以看到“test.common”jar,但后端模块不会导入它。希望你能帮帮我。

*更新*

将公共依赖项移动到依赖项部分:

  <dependencies>
    ...
    <dependency>
        <groupId>test</groupId>
        <artifactId>common</artifactId>
        <version>${project.parent.version}</version>
        <scope>compile</scope>
    </dependency>
  </dependencies>

祝你好运 Ioannis K。

1 个答案:

答案 0 :(得分:0)

哦,天啊!我没有意识到我将公共依赖项放在dependencyManagement部分。

解决问题:将公共依赖项从dependencyManagement移动到依赖项部分。

祝你好运 Ioannis K。