【问题标题】:Maven multiple source directory module not workingMaven多源目录模块不起作用
【发布时间】:2016-01-13 03:06:01
【问题描述】:

我有一个多模块项目,其中一些模块有多个源目录(一个用于手工创建的代码,一个用于生成的代码)。

项目如下:

Product
+-- pom.xml
+-- MagniCompCommon
   +-- src/main/java
   +-- src-gen
   +-- pom.xml

我正在使用 build-helper-maven-plugin 添加 src-gen 目录。但是,当我在 Parent maven 中通过“mvn clean install”编译时,它不会编译 src-gen 中的任何内容,也不会在尝试编译 src/main/java 文件时将其包含在类路径中。由于src/main/java中的很多java代码引用了src-gen代码,导致编译失败。

这里是父 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>

  <groupId>com.magnicomp</groupId>
  <artifactId>Product</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>


  <modules>
    <module>MagniCompCommon</module>
    <module>Model</module>
    <module>Common</module>
    <!-- <module>Agent</module> -->
    <!-- <module>Doc</module> -->
  </modules>

    <properties>
        <!-- MagniComp common -->
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hibernate.version>4.3.11.Final</hibernate.version> <!-- was 4.3.10.Final -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- Product specific -->
        <!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -->
        <jersey.glassfish.version>2.21</jersey.glassfish.version>
        <javax.servlet.version>3.0</javax.servlet.version>
        <bouncy.version>1.51</bouncy.version>       
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                      <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                          <goal>add-source</goal>
                        </goals>
                        <configuration>
                          <sources>
                            <source>src-gen</source>
                          </sources>
                        </configuration>
                      </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <!-- Use Java 8 (default is 5) -->
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

我也尝试将${project.basedir}/src-gen 用于&lt;source&gt;,但这没有任何区别。

这里是 MagniCompCommon pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.magnicomp</groupId>
        <artifactId>Product</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>MagniCompCommon</artifactId>
    <packaging>jar</packaging>
.... snip ...

【问题讨论】:

  • src-gen 中的那些东西是通过哪些工具生成的? (XSD/REST/WSDL?)

标签: java maven


【解决方案1】:

移动到孩子:

   <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                      <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                          <goal>add-source</goal>
                        </goals>
                        <configuration>
                          <sources>
                            <source>src-gen</source>
                          </sources>
                        </configuration>
                      </execution>
                    </executions>
                </plugin>
</plugins>
  • 为什么使用 pluginManagement => 只是共享同一个插件的一种方式。
  • maven-compiler-plugin 你可以把它放在pluginManagement 中而不是其他插件。

【讨论】:

    【解决方案2】:

    父POM有

    <packaging>pom</packaging>
    

    这意味着这个 POM 没有正常的构建生命周期,那里没有代码。

    build-helper-maven-plugin 描述从父项目移动到MagniCompCommon 的POM,在build.plugins build.pluginmanagement.plugins

    编辑:另外,如果您没有将src-gen 签入源代码管理(并且由于它已生成,最好不要签入),请改用target/generated-sources,然后就不需要build-helper-maven-plugin

    【讨论】:

    • 我确实试过了,但没有效果。我将整个 ... 内容从 Parent pom.xml 移动到 MagniCompCommon pom.xml。我之前还尝试将 maven-compiler-plugin 部分留在 Parent pom.xml 中,并将 build-helper-maven-plugin 移动到 MagniCompCommon pom.xml。那也没有效果。
    • 你没有没有完整阅读我的评论。您的问题源于您在pluginManagement 中设置了插件描述,这表明插件的默认值,如果在构建中引用了该插件在子项目中。将其移至您的子项目,在 plugins 标记中。
    • 啊!是的,你是对的。我错过了。现在效果很好。非常感谢!
    【解决方案3】:

    这里是 MagniCompCommon pom.xml 的工作版本:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src-gen</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

      【解决方案4】:

      创建多模块 maven 项目(与您的相同)并将以下构建标签添加到子模块 MagniCompCommon pom.xml 文件中。对我来说工作正常,没有编译错误

      <build>
          <plugins>
              <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
              <includes>
                  <include>src/main/java/**/*.java</include>
                  <include>src-gen/**/*.java</include>
              </includes>
              </configuration>
              </plugin>
          </plugins>
      </build>`
      

      【讨论】:

        猜你喜欢
        • 2015-11-29
        • 2013-12-13
        • 2013-10-30
        • 2011-11-24
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        相关资源
        最近更新 更多