mavenjarmaven-antrun-plugingmaven-plugin

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


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


Solution

  • 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!:)