簡體   English   中英

Maven:在打包時創建單獨的 jar 文件是 pom

[英]Maven: create separate jar file while packing is pom

我試圖讓 Maven 創建一個單獨的jar僅包含resources文件夾。 項目結構如下(沒有main文件夾所以沒有main類):

db
src
|--resources
   |--flyway

pom.xml看起來像這樣(請注意,我知道packaging設置為pom ,因為它必須保持這種狀態):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>My Project</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classesDirectory>src</classesDirectory>
                    <includes>
                        <include>
                            none of the paths worked
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jfrog.buildinfo</groupId>
                <artifactId>artifactory-maven-plugin</artifactId>
                <version>3.0.0</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>build-info</id>
                        <goals>
                            <goal>publish</goal>
                        </goals>
                        <configuration>
                            <publisher>
                                <contextUrl>http://mvn.mycompany/artifactory</contextUrl>
                                <username>${username}</username>
                                <password>${password}</password>
                                <repoKey>libs-release-local</repoKey>
                                <snapshotRepoKey>libs-snapshot-local</snapshotRepoKey>
                            </publisher>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray-plugins</name>
            <url>https://jcenter.bintray.com</url>
        </pluginRepository>
    </pluginRepositories>
</project>

我嘗試使用maven-jar-plugin但不幸的是我無法讓它工作,以便它為我生成一個包含resources/flyway .jar文件夾的 .jar 文件。 pom.xml包裝必須保留pom ,所以我無法將其切換回jar (它顯然可以解決我的問題)。 我怎么能生產 jar package 是這樣的情況,所以它最終會在.m2文件夾中?

所有這一切的原因是我想為我的 flyway 腳本使用單獨jar package,我可以在任何我想要的地方堅持這種依賴。

Maven 資源的默認位置是src/main/resources ,測試資源的默認位置是src/test/resources 最佳做法是盡可能遵循 Maven 約定。

如果由於某種原因您不能使用標准目錄結構,那么您需要添加一個<resources>元素

<project>
...
   <build>
   ...
   <resources>
      <resource>
         <directory>src/resources</directory>
      </resource>
   </resources>

   <plugins>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
       </plugin>

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <!-- jar, war, ear, assembly.... -->
            <artifactId>packaging-plugin-here</artifactId>
       </plugin>

   </plugins>
   </build>
...
</project>

你有可能創建一個單獨的模塊嗎? 如果是這樣,您可以保留 pom 包裝並執行以下操作:

<packaging>pom</packaging>
<modules>
    <module>resources</module>
</modules>

在“資源”模塊中,您可以使用帶有 jar 包裝的 pom.xml。

我同意那些說最好把它放在一個單獨的模塊中的評論。 盡管如此,這不應該阻止你學習如何做到這一點。 maven 組裝插件使這變得簡單:

在 src/main/resources/flyway 中有 flyway 腳本的項目,有這個 pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/flyway.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

引用 src/main/assembly/flyway.xml 中的“程序集配置”:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
  <id>flyway</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
        <directory>src/main/resources/flyway</directory>
    </fileSet>
  </fileSets>
</assembly>

將在 package 時間生成一個包含您的飛行腳本的 zip 文件。 (如果你真的想堅持 src/resources/flyway 你可以很容易地適應它)

請注意,雖然此示例使用 pom 作為包裝,但它也適用於 packaging=jar; 它只會在主要工件旁邊創建第二個工件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM