将生成的源代码作为源文件夹添加到Eclipse

8

我正在使用maven-jaxb-plugin根据xsd文件生成类文件源代码:

<plugin>
                <groupId>com.sun.tools.xjc.maven2</groupId>
                <artifactId>maven-jaxb-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <id>jaxb-xsd-constants</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generatePackage>com.mypackage</generatePackage>
                            <schemaDirectory>${basedir}/src/main/resources/xsd/mylist</schemaDirectory>
                            <includeSchemas>
                                <includeSchema>mylist.xsd</includeSchema>
                            </includeSchemas>
                            <strict>true</strict>
                        </configuration>
                    </execution>                    
                </executions>
            </plugin>

enter image description here

然后我需要将这些文件夹添加为源文件夹,以便Eclipse在加载和编译它们时可以使用:

如何使用插件或其他方法将文件夹添加为源文件夹?不必手动添加这些文件夹。

3个回答

9
尝试使用这个Maven插件...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/xjc</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

4

两种选项:

  • 使用像 maven-jaxb2-plugin 这样的现代插件,该插件会自动添加源目录(maven-jaxb2-plugin 可以实现这一点)。
  • 使用类似于 buld-helper-maven-plugin 的东西,来添加源文件夹

免责声明: 我是上面提到的 maven-jaxb2-plugin 的作者。


当我使用build-helper-maven-plugin时,我需要针对当前项目的pom运行mvn eclipse:eclipse。这是预期的行为吗?如果我不运行eclipse:eclipse,则该目录不会被添加为源文件夹。 - blue-sky
@blue-sky 我个人使用m2eclipse和maven-jaxb2-plugin,在eclipse中自动工作。我从来不运行mvn eclipse:eclipse。 - lexicore

0

我曾经在使用vert.x代理服务时遇到了一些困难,无法添加一些生成的文件。以下是我在Eclipse中将生成的文件添加到项目中所遵循的步骤:

  1. 右键单击您的生成文件夹(在您的情况下为mylist
  2. 点击“构建路径”
  3. 然后点击“用作源文件夹”

就这样!Eclipse将添加包含生成文件的文件夹。这是在添加生成文件后我的项目结构的样子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接