简体   繁体   English

Maven多模块项目导入模块

[英]Maven multi module project import module

I read a lot of tutorials for the last 3 days with no success. 最近三天,我读了很多教程,但都没有成功。

I have a maven multi module project. 我有一个Maven多模块项目。 In the parent pom I define my submodules: 在父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>

The common module keeps common data. 通用模块保留通用数据。 Basically it provides google protobuf classes that will be used in my backend (and later also client) project. 基本上,它提供了Google protobuf类,这些类将在我的后端(以及以后的客户端)项目中使用。 So, my protobufs are being generated and now I want to use them in my backend project. 因此,我的protobuf正在生成,现在我想在我的后端项目中使用它们。

pom.xml common: 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 backend: In the dependencies section I define the common project to be included. 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>

Now, I am using mvn install on the parent project to generate and copy the jars to my local repository. 现在,我在父项目上使用mvn install来生成jar并将其复制到本地存储库。 (I also tried to invoke the command on the common project first). (我也尝试首先在公共项目上调用命令)。 Everything is being generated fine but I can not use my protobufs and other classes in backend module.. 一切都生成良好,但是我不能在后端模块中使用protobufs和其他类。

The dependency tree of the backend module does not even show up my "test.common" dependency. 后端模块的依赖关系树甚至没有显示我的“ test.common”依赖关系。 Thats why I can not include the classes. 那就是为什么我不能包含这些类。 Its very frustrating! 它非常令人沮丧! I tried a lot, eg, to use the maven compiler plugin in my common pom: 我做了很多尝试,例如,在我的通用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>

But it stills does not work. 但是它仍然不起作用。 Do I miss anything? 我想念什么吗? When looking to my local repository I can see the "test.common" jar but the backend module does not import it. 当查看我的本地存储库时,我可以看到“ test.common” jar,但后端模块不会导入它。 Hope you can help me. 希望您能够帮助我。

* UPDATE * *更新*

Moving common dependency to dependencies section: 将公共依赖项移到依赖项部分:

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

Best regards Ioannis K. 最好的问候Ioannis K.

Oh my gosh! 天啊! I didnt realize I put the common dependency to the dependencyManagement section. 我没有意识到我将公共依赖项放到dependencyManagement部分。

To solve the problem: move the common dependency from dependencyManagement to dependencies section. 解决此问题的方法:将公共依赖项从dependencyManagement移到dependencies部分。

Best regards Ioannis K. 最好的问候Ioannis K.

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

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