如何将单个java文件作为源/从属文件包括在内

z4iuyo4d  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(281)

我在maven中创建了一个多模块项目,如下所示:

root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
|________common/pom.xml
         |________/src/main/java
|________tools/pom.xml
         |________/src/main/java
|________server/pom.xml
         |________/src/main/java
         |________/src/main/resources

我想编译“client”模块代码,它依赖于“common”模块中的所有java类,但依赖于“tools”模块中的一些java类。使用下面的buildhelpermaven插件,我可以将所有java源文件添加到common/path下,但是我需要一种方法将单个java文件定义为tools/下的源文件。

<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>../common/src/main/java</source>
                    <!-- Adding single java files as below does not work -->
                    <source>../tools/log/Log.java</source>
                    <source>../tools/socket/SocketClient.java</source>
                    <!----------------------------------------------------->
                </sources>
            </configuration>
       </execution>
    </executions>
mec1mxoz

mec1mxoz1#

好的,根据收到的评论,理想的解决方案似乎是将模块分解成子模块,并将其用作依赖项。但是,由于这是一项耗时的任务,我将发布一个快速而肮脏的解决方案,介绍如何将一个或多个java文件复制到新的源目录src/main/external下,然后将此目录作为源代码的一部分添加到构建中。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
               <execution>
                  <id>copy-source-java-files</id>
                  <phase>generate-resources</phase>
                  <goals>
                     <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>src/main/external</outputDirectory>
                     <overwrite>true</overwrite>
                     <resources>
                        <resource>
                           <!-- External source directory -->
                           <directory>../tools/src/main/java/</directory>
                           <includes>
                              <!-- Files to be copied along with their directory paths -->
                              <include>tools/log/Log.java</include>
                              <include>tools/socket/SocketClient.java</include>
                              <include>tools/client/reader/**</include>
                           </includes>
                        </resource>
                     </resources>
                  </configuration>
               </execution>
            </executions>
         </plugin>

         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/java</source>
                            <source>src/main/resources</source>
                            <source>../common/src/main/java</source>
                            <!-- Here we add the source with the files copied -->
                            <source>src/main/external</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
  </build>

编译后的输出如下:

root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
         |________/src/main/external  <----- This contains all external java files.
uxh89sit

uxh89sit2#

你应该在“客户端”中包含模块“common”和“tools”作为依赖项,然后你可以在project:mvn clean install的根目录下运行build-它将构建所有模块。

qkf9rpyu

qkf9rpyu3#

不要试图使用模块的某些部分作为依赖项。maven不支持这一点,它会导致结构脆弱且难以维护。
如果你只需要一部分 tools ,那么这是一个明确的信号 tools 太大了,你需要把它分成两个模块。

相关问题