javamavenjar

Overriding Maven output JAR with a pre-built JAR


I am new to Maven and am trying to override the generated JAR with a pre-built one.

I am actually converting a very old Ant project to Maven, and there is this pre-built JAR that's very old and I don't know where's the source for this pre-built JAR, but this pre-built JAR is being used as a dependency in other modules.

I have tried using the maven-assembly-plugin that would copy the pre-built JAR, but it gets copied in to the JAR that is generated by Maven (JAR within a JAR). I tried disabling the maven-jar-plugin explicitly but when using the maven-assembly-plugin, it always generates the default one, and the pre-built JAR gets copied inside this JAR.

Is there a way so that my pre-built JAR becomes the generated JAR? I tried using maven-install-plugin as well but that got too complex and I decided to stick with maven-assembly-plugin.

Here's my assembly:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 https://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>prebuilt-jar</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/lib</directory>
            <includes>
                <include>myjar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Here's my pom.xml

<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>

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>SomeArtifact</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>myartifact</artifactId>
    <packaging>jar</packaging>
    <name>myartifact</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.7.1</version>
                <executions>
                    <execution>
                        <id>package-prebuilt-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/assembly/myassembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Solution

  • If you have an existing JAR that you want to use as dependency, you can either

    1. install it locally using the maven install plugin.
    2. upload/deploy it to your Nexus or Artifactory if you have one.