How to register a custom built jar file as maven main artifact?

我们两清 提交于 2019-12-10 01:38:24

问题


I have a project expected to deliver a jar file:

<packaging>jar</packaging>

but the jar is built in a custom way, so the default packaging done with jar:jar has been disabled

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.3.2</version>
  <executions>
    <execution>
      <id>default-jar</id>
      <phase>none</phase>
    </execution>
  </executions>
</plugin>

but then when I want to apply shade:shade on the existing jar I get an error

The project main artifact does not exist.

I assume that maven doesn't know about the .jar file created by my custom tool. How to let it know, because antrun attachArtifact doesn't work

<attachartifact file="./bin/classes.jar" classifier="" type="jar"/>

the error I get is

An Ant BuildException has occured: org.apache.maven.artifact.InvalidArtifactRTException: For artifact {:jar}: An attached artifact must have a different ID than its corresponding main artifact.

So this is not the method to register main artifact... Is there any (without writing custom java plugin)?

Thanks, Lukasz


回答1:


I checked the sources of JarMojo and it gave me an idea how to solve it with Groovy (via gmaven)

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>set-main-artifact</id>
      <phase>package</phase>
      <goals> 
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          project.artifact.setFile(new File("./bin/classes.jar"))
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>

and it works!:)




回答2:


Something like this

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>attach-artifact</goal>
            </goals>
            <configuration>
                <artifacts>
                    <artifact>
                        <file>${basedir}/bin/classes.jar</file>
                        <type>jar</type>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>



回答3:


While your solution may work for a build to the install+ phase or where there are no dependencies in the reactor, in cases where only building to the compile or test phase the unpackaged classes won't be found by dependencies. Building to compile happens when using plugins like the maven-release-plugin.

Extending your chosen solution to include identifying the unpacked classes during compile

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>set-main-artifact-compile</id>
      <phase>compile</phase>
      <goals> 
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          project.artifact.setFile(new File("./bin/classes"))
        </source>
      </configuration>
    </execution>
    <execution>
      <id>set-main-artifact</id>
      <phase>package</phase>
      <goals> 
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          project.artifact.setFile(new File("./bin/classes.jar"))
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>

By default the maven-install-plugin will use the identified artifact along the lines of ${project.build.directory}/${project.finalname}.jar

So another option might go something like this

<build>
    <directory>bin</directory>
    <outputDirectory>bin/classes</outputDirectory>
    <finalName>classes</finalName>
</build>



回答4:


We were having the same problem, with getting the "attached artifact must have a different ID than its corresponding main artifact" error. We found the solution in the following excellent blog post:

http://devblog.virtage.com/2013/04/embed-and-run-ant-tasks-and-scripts-from-maven/

As detailed in this section, you can fix the problem by adding a classifier so Maven can distinguish between the ant-built jar and the maven-built jar. Since you're using antrun attachartifact, you'd need this:

<attachartifact file="./bin/classes.jar" classifier="foo" type="jar"/>

Note you'll also need to include that classifier (along with groupId, artifactId and version) whenever you want to grab this jar as a dependency in other projects.



来源:https://stackoverflow.com/questions/10449216/how-to-register-a-custom-built-jar-file-as-maven-main-artifact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!