java 如何让maven从另一个项目中复制所有类?

aiqt4smr  于 2023-05-15  发布在  Java
关注(0)|答案(2)|浏览(107)

我在一个共享项目中有许多Entity类。
为了让JPA正常工作,我需要将这些类文件放在classes文件夹中,而不是jar中。
我该如何在maven中做到这一点呢?我可以使用特定的maven插件吗?

来源:

project1/src/main/java/org/something/SomeClass.java

project2/src/main/java/org/somethingElse/SomeEntity.java

目标:

project1/target/classes/org/something/SomeClass.java
project1/target/classes/org/somethingElse/SomeEntity.java

其他方法也是OK的。

更新:

使用案例:
我有多个项目。
一个项目是所有共享内容的核心库。
这包括一堆注入了PersistanceUnit Entity Managers的实体类。
当我尝试使用这些实体类时,问题就出现了,因为实体管理器不再被注入。另外,当我手动指定实体管理器时,它不能识别共享jar中的任何实体类。

2izufjch

2izufjch1#

尝试Unpacking specific artifacts
文档中的示例配置:

<project>
   [...]
   <build>
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.10</version>
         <executions>
           <execution>
             <id>unpack</id>
             <phase>package</phase>
             <goals>
               <goal>unpack</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>junit</groupId>
                   <artifactId>junit</artifactId>
                   <version>3.8.1</version>
                   <type>jar</type>
                   <overWrite>false</overWrite>
                   <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                   <destFileName>optional-new-name.jar</destFileName>
                   <includes>**/*.class,**/*.xml</includes>
                   <excludes>**/*test.class</excludes>
                 </artifactItem>
               </artifactItems>
               <includes>**/*.java</includes>
               <excludes>**/*.properties</excludes>
               <outputDirectory>${project.build.directory}/wars</outputDirectory>
               <overWriteReleases>false</overWriteReleases>
               <overWriteSnapshots>true</overWriteSnapshots>
             </configuration>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>
   [...]
 </project>
djp7away

djp7away2#

您可以尝试使用**copysource-maven-plugin**:

(...)
<build>
   <plugins>
      (...)
      <plugin>
         <groupId>org.bytemechanics.maven</groupId>
         <artifactId>copysource-maven-plugin</artifactId>
         <version>X.X.X</version>
         <executions>
            <execution>
               <goals>
                  <goal>copy-classes</goal>
               </goals>
               <configuration>
                  <copies>
                     <copy>
                        <artifact>[source-groupId]:[source-artifactId]:[source-version]</artifact>
                        <classes>
                           <class>[cannonical-name-of-origin-source. Example:org.bytemechanics.commons.functional.LambdaUnchecker]</class>
                           (...)
                        </classes>
                        <fromPackage>[package-segment-to-replace. Example: org.bytemechanics.commons]</fromPackage>
                        <toPackage>[package-segment-to-replace. Example: org.bytemechanics.standalone.ignite.internal.commons]</toPackage>
                     </copy>
                  </copies>
               </configuration>
            </execution>
         </executions>
      </plugin>     
      (...)
    </plugins>
</build>    
(...)

相关问题