【问题标题】:Maven compiler plugin, build helper plugin does not generate class filesmaven编译插件,build helper插件不生成class文件
【发布时间】:2012-11-21 21:01:24
【问题描述】:

我正在尝试将现有项目转换为 Maven 项目。它有 3 个模块,其中一个模块有多个源文件夹。 http://i.imgur.com/jZCnR.png

maven clean and install 或 eclipse clean 不会在 classes 和 test-classes 文件夹中创建类文件。项目结构是由于那里没有类文件而创建的。

下面的插件配置是在父pom中定义的。

<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/main/java/**/*.*</source>
                    <source>src/report/java/**/*.*</source>
                    <source>src/architect/java/**/*.*</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals><goal>add-test-source</goal></goals>
            <configuration>
                <sources>
                    <source>src/test/java/**/*.*</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <debug>false</debug>
    </configuration>
    <executions>
        <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals><goal>compile</goal></goals>
            <configuration>
                <includes>
                    <include>src/main/java/**/*.*</include>
                    <include>src/report/java/**/*.*</include>
                    <include>src/architect/java/**/*.*</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals><goal>testCompile</goal></goals>
            <configuration>
                <testIncludes>
                    <include>src/test/java/**/*.*</include>
                </testIncludes>
            </configuration>
        </execution>
    </executions>
</plugin>

我做错了什么?

【问题讨论】:

  • 为什么要添加与 src/main/java 或 src/test/java 不同的位置?这真的有必要吗?
  • 因为在这个遗留项目中有多个源文件夹,并且它们之间存在双向依赖关系。如果我做了一个特定的来源,比如说前。在这种情况下,“report”src 文件夹,作为一个单独的 maven 模块,那么我不知道如何解决这个双向依赖。
  • 根据您提供的信息,您需要将多个源文件夹分成单独的模块。
  • 好吧,如果我这样做,并说我创建了一个单独的模块 _GAS2Report 和 _GAS2WebApp 取决于 _GAS2Report 中存在的某些功能,反之亦然。那么我应该在它们中添加一个依赖项吗?
  • 如果你有这样的依赖,那就是。但是如果你有一个循环引用,那就有问题了......

标签: maven maven-2 m2eclipse maven-compiler-plugin


【解决方案1】:

对我来说,eclipse 中的 mvn 插件也不会生成正确的结构。 尝试在 cmd 中运行 mvn eclipse:eclipse。这对我有用。

您还使用了“生成测试源”阶段。这似乎不起作用尝试“生成源”。

【讨论】:

  • 如果有人知道为什么 generate-test-sources 不起作用。在评论中添加它。
  • 简短的回答是你需要在 'maven-eclipse-plugin' 执行之前执行 'generate-test-sources' 目标,所以运行 'mvn generate-test-sources eclipse:eclipse'。我想在这里更详细地回答:stackoverflow.com/a/20463195/1410035
【解决方案2】:

根据Using MOJO build-helper-maven-plugin to add more source folders to MAVEN,您应该通过包含标签再次定义其他源目录;只需在 build-helper-maven-plugin 中定义它们,就是这样。

您也可以尝试将 add-source 目标与早期的 Maven 阶段相关联,例如 initialize。一般来说,越早越好。

<phase>initialize</phase>
<goals>
    <goal>add-source</goal>
</goals>

就我而言,我的错误是将其绑定到 compile 阶段。尽管我的 build-helper-maven-plugin 是在 pom.xml 中的 maven-compile-plugin 之前定义的,但不知何故 default- compile 任务首先启动,并默默地忽略了我额外的源目录,从而在构建过程中进一步导致了一些意外错误。绑定到同一阶段的步骤顺序应遵循 pom.xml 中的出现顺序,但并非总是如此。

检查您在日志中看到的内容:

[INFO] --- build-helper-maven-plugin:1.8:add-source (add-gen-source) @ deepclone-sample-project-1 ---
[INFO] Source directory: E:\dev\workspace...\src\main\generated added.

小窍门:在代码中故意犯错,只是为了检查编译器是否真的通过了额外的目录。如果编译没有立即失败,那么问题不在于不生成类文件,而在于编译器配置。您可以排除增量构建问题或类似的更深奥的问题。

【讨论】:

    【解决方案3】:

    作为build helper maven pluginadd-source目标中的documented,您应该在&lt;source&gt;标签中指定一个目录

    【讨论】:

    • 谢谢,我修好了。所以现在我所有的资源都被正确地复制到了 classes 和 test-classes 文件夹中。但是我仍然没有任何类文件。
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2014-07-27
    • 2012-03-13
    相关资源
    最近更新 更多