2016-02-29 324 views
0

我在过去的3天里阅读了很多教程,但没有成功。Maven多模块项目导入模块

我有一个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”依赖项。这就是为什么我不能包括课程。它非常令人沮丧!我尝试了很多,例如,要使用maven编译器插件在我的共同POM:

<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,但后端模块不会导入它。希望您能够帮助我。

* UPDATE *

移动共同依赖关系的依赖部分:

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

问候 扬K.

回答

0

哦,我的天哪!我没有意识到我把公共依赖关系放在dependencyManagement部分。

要解决该问题:将公共依赖关系从dependencyManagement移动到依赖关系部分。

祝你好运 Ioannis K.