【问题标题】:Maven multi module project import moduleMaven多模块项目导入模块
【发布时间】:2016-06-12 13:04:42
【问题描述】:

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

通用模块保存通用数据。基本上它提供了 google protobuf 类,这些类将在我的后端(以及后来的客户端)项目中使用。 所以,我的 protobuf 正在生成,现在我想在我的后端项目中使用它们。

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 并将其复制到我的本地存储库。 (我也尝试先在 common 项目上调用命令)。一切都很好,但我不能在后端模块中使用我的 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>

最好的问候 爱奥尼斯 K.

【问题讨论】:

    标签: maven inheritance module dependencies project


    【解决方案1】:

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

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

    最好的问候 爱奥尼斯 K.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-11
      • 2014-09-14
      • 2023-03-25
      • 2018-02-06
      • 1970-01-01
      • 2018-10-15
      • 2021-02-09
      • 2011-01-04
      相关资源
      最近更新 更多