You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成类似ossrh-test-1.2.pom的文件并纳入最终JAR包?

Hey there! Let's tackle your questions one by one to get that versioned POM generated in target and packed into your JAR as required by Sonatype.

1. Why isn't the POM appearing in your target directory?

By default, Maven installs your project's POM directly to your local repository (that's the one you found in ~/.m2/repository/mystuff-maven-plugin/) when you run mvn clean install, but it doesn't automatically copy a versioned copy to the target folder. We need to add a bit of plugin configuration to make that happen.

2. How to generate a versioned POM like ossrh-test-1.2.pom in target?

You can use either the Maven Resources Plugin or Maven AntRun Plugin to copy and rename your root pom.xml to the target directory during the build lifecycle. Here are both options:

Option 1: Maven Resources Plugin (recommended)

Add this to your project's pom.xml under the <build><plugins> section:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.3.1</version>
  <executions>
    <execution>
      <id>copy-versioned-pom-to-target</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <resources>
          <resource>
            <directory>${project.basedir}</directory>
            <includes>
              <include>pom.xml</include>
            </includes>
            <destFileName>${project.artifactId}-${project.version}.pom</destFileName>
            <!-- Set filtering to true if you need to replace placeholders in the POM; leave false otherwise -->
            <filtering>false</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

This runs during the prepare-package phase (right before packaging your JAR), copies your root pom.xml to target, and renames it to match the pattern [artifactId]-[version].pom (e.g., ossrh-test-1.2.pom).

Option 2: Maven AntRun Plugin

If you prefer using Ant tasks, this works too:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>create-versioned-pom</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <tasks>
          <copy 
            file="${project.basedir}/pom.xml" 
            tofile="${project.build.directory}/${project.artifactId}-${project.version}.pom" />
        </tasks>
      </configuration>
    </execution>
  </executions>
</plugin>

3. How to include this versioned POM into your final JAR?

Once you're generating the POM in target, you need to tell the Maven JAR Plugin to include it in the final JAR. Add this configuration to your pom.xml (either alongside the plugin above or in an existing maven-jar-plugin entry):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.3.0</version>
  <configuration>
    <includes>
      <!-- Include all .pom files in target; or specify the exact filename if you want -->
      <include>*.pom</include>
    </includes>
    <!-- Optional: If you want to place the POM in a specific folder inside the JAR (like META-INF) -->
    <!-- <archive>
      <manifestEntries>
        <POM-Location>META-INF/${project.artifactId}-${project.version}.pom</POM-Location>
      </manifestEntries>
    </archive> -->
  </configuration>
</plugin>

If you want the POM in a specific directory (like META-INF), adjust the destFileName in the Resources Plugin to include the path (e.g., destFileName>META-INF/${project.artifactId}-${project.version}.pom</destFileName>) — the JAR plugin will automatically include files in META-INF by default.

Final Step

Run mvn clean install again, and you'll see your versioned POM (like ossrh-test-1.2.pom) in the target directory. It will also be packed inside your final JAR file, which satisfies Sonatype's requirements.

内容的提问来源于stack exchange,提问作者mhradek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:59:20