Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven-如何将单个java文件作为源/依赖项包含_Java_Maven_Maven Plugin - Fatal编程技术网

Maven-如何将单个java文件作为源/依赖项包含

Maven-如何将单个java文件作为源/依赖项包含,java,maven,maven-plugin,Java,Maven,Maven Plugin,我在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 |___

我在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
root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
         |________/src/main/external  <----- This contains all external java files.
我想编译“客户机”模块代码,它依赖于“通用”模块中的所有java类,但依赖于“工具”模块中的一些java类。 使用如下所示的buildhelpermaven插件,我能够在common/路径下添加所有java源文件,但我需要一种方法将单个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>
org.codehaus.mojo
构建助手maven插件
3.2.0
生成源
添加源
../common/src/main/java
../tools/log/log.java
../tools/socket/SocketClient.java
您应该在“客户端”中包含模块“通用”和“工具”作为依赖项
然后,您可以在project:mvn clean install的根目录下运行build,它将生成所有模块。

不要尝试将模块的某些部分用作依赖项。Maven不支持这一点,它会导致结构易碎且难以维护


如果您只需要部分
工具
,那么这是一个明确的信号,表明
工具
太大,您需要将其分为两个模块。

好的,根据收到的评论,理想的解决方案似乎是将模块分为子模块,并将其用作依赖项。然而,由于这目前是一项耗时的任务,我正在发布一个快速而肮脏的解决方案,介绍如何将一个或多个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>

maven资源插件
3.2.0
复制源java文件
产生资源
复制资源
src/主/外部
真的
../tools/src/main/java/
tools/log/log.java
tools/socket/SocketClient.java
工具/客户端/阅读器/**
org.codehaus.mojo
构建助手maven插件
3.2.0
生成源
添加源
src/main/java
src/main/resources
../common/src/main/java
src/主/外部
汇编后的结果如下:

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
root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
         |________/src/main/external  <----- This contains all external java files.
root/pom.xml
|________client/pom.xml
|________/src/main/java
|________/src/main/resources

|________/src/main/externaltools模块具有客户端所需的java文件和服务器模块所需的其他文件。在工具中的100个java文件中,我需要为客户端使用10个特定的文件。我不需要包括所有这些,因为很多都是不相关的,也依赖于我不需要的其他职业,所以你将与Maven战斗,这是一场你将失去的战斗。。。简单地清理你的结构,遵循最佳实践,而不是规避它们,因为你相信你更了解它……不完全是这样。Tools有各种工具,可以从任何其他模块使用,也可以不从任何其他模块使用。它就像一个有很多功能的大罐子,但我只需要使用必要的功能来保持小尺寸,把不必要的东西放在外面。在使用maven之前,我可以通过“链接源”特性在eclipse上实现这一点,而我可以将单个文件作为projectIn maven的一部分进行链接—使用依赖项的一部分是没有意义的。要么你把
工具
拆了,要么你就带着不需要的东西生活。通常情况下,如果你有10个或100个额外的课程,这并不重要。如果依赖性是个问题,我会排除
工具的一些依赖性。Eclipse是一个不好的构建工具,它不能在Maven正是为了实现这一目的的情况下实现自动化…看起来好像有人对Maven进行了一次转换,只提供了一半的支持,并没有真正正确地理解需要什么。。。此外,fat-jar是导致其他问题的一条途径,您不喜欢这些问题……(在评论中解释得太长了。)……这根本不起作用,因为您的结构完全错误。您应该创建一个由其他模块所需的类组成的模块。这可能会导致许多只包含一个或两个类的小模块,但这使模块更易于重用,并为Maven依赖项等打开了道路。这也有助于以后提高构建性能等。但Helper完全是错误的方法。只需制作一个单独的模块
公共模块
,其中仅包含这些类是的一部分,然后您可以通过Maven的pom文件向该模块添加依赖项,就是这样。。。