Search code examples
javadockermavengradle

Third party Jar pom.xml


We have a third party jar sitting in internal maven artifactory. We have to wrap it as docker image and persist in a docker registry. We have CI process in place to build a maven project and wrap it with docker and deploy it. But in this case, there is no additional java build process required as its a runnable 3rd party jar.

I know as part of CI step, we can do this and then continue to docker build

mvn dependency:copy -Dartifact=<org:artifact:version> -DoutputDirectory=<Path>/target

But this needs a custom CI step just to accommodate this. Question: Is there anyway that we can avoid this special CI step, probably by using a dummy wrapper pom.xml to download the thirdparty jar and make it as the build output.

Not married to maven, it can be gradle too.


Solution

  • The "copy" part seems to be trivial: you just need to add corresponding maven-dependency-plugin configuration to pom.xml and assign proper lifecycle phase to copy goal, for example, the configuration below:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
      <execution>
       <id>copy</id>
       <goals>
        <goal>copy</goal>
       </goals>
       <phase>prepare-package</phase>
       <configuration>
        <artifactItems>
         <artifactItem>
          <groupId>tld.group</groupId>
          <artifactId>artifact</artifactId>
          <version>version</version>
          <outputDirectory>${project.build.directory}</outputDirectory>
          <destFileName>copy-of-artifact.jar</destFileName>
         </artifactItem>
        </artifactItems>
       </configuration>
      </execution>
     </executions>
    </plugin>
    

    causes maven to copy required jar into target directory during build lifecycle.

    As regards to "mark third party jar as the build output" part, there are a couple of options:

    I. basically, you may unpack third party jar into classes directory and ask maven-jar-plugin do not create manifest:

    
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <configuration>
      <useDefaultManifestFile>true</useDefaultManifestFile>
     </configuration>
    </plugin>
    
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
      <execution>
       <id>unpack</id>
       <goals>
        <goal>unpack</goal>
       </goals>
       <phase>prepare-package</phase>
       <configuration>
        <artifactItems>
         <artifactItem>
          <groupId>tld.group</groupId>
          <artifactId>artifact</artifactId>
          <version>version</version>
          <outputDirectory>${project.build.directory}/classes</outputDirectory>
         </artifactItem>
        </artifactItems>
       </configuration>
      </execution>
     </executions>
    </plugin>
    

    II. another option is to trick maven build via disabling maven-jar-plugin and modifying project's state using gmavenplus-plugin:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <executions>
      <execution>
       <id>default-jar</id>
       <phase>none</phase>
      </execution>
     </executions>
     <configuration>
      <skip>true</skip>
     </configuration>
    </plugin>
    
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
      <execution>
       <id>copy</id>
       <goals>
        <goal>copy</goal>
       </goals>
       <phase>prepare-package</phase>
       <configuration>
        <artifactItems>
         <artifactItem>
          <groupId>tld.group</groupId>
          <artifactId>artifact</artifactId>
          <version>version</version>
          <outputDirectory>${project.build.directory}</outputDirectory>
          <destFileName>${project.build.finalName}.jar</destFileName>
         </artifactItem>
        </artifactItems>
       </configuration>
      </execution>
     </executions>
    </plugin>
    
    <plugin>
     <groupId>org.codehaus.gmavenplus</groupId>
     <artifactId>gmavenplus-plugin</artifactId>
     <version>1.13.1</version>
     <executions>
      <execution>
       <phase>package</phase>
       <goals>
        <goal>execute</goal>
       </goals>
       <configuration>
        <bindAllProjectProperties>true</bindAllProjectProperties>
        <scripts>
         <script><![CDATA[
    project.artifact.file = new File("${project.build.directory}/${project.build.finalName}.jar")
      ]]></script>
        </scripts>
       </configuration>
      </execution>
     </executions>
     <dependencies>
      <dependency>
       <groupId>org.codehaus.groovy</groupId>
       <artifactId>groovy-all</artifactId>
       <version>3.0.9</version>
       <type>pom</type>
       <scope>runtime</scope>
      </dependency>
     </dependencies>
    </plugin>