java - Overriding Maven output JAR with a pre-built JAR - Stack Overflow

admin2025-05-02  2

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=".1.2"
          xmlns:xsi=";
          xsi:schemaLocation=".1.2 .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=".0.0" xmlns:xsi=";
         xsi:schemaLocation=".0.0 .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>

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>
Share Improve this question edited Jan 3 at 18:13 nullpointer asked Jan 2 at 13:24 nullpointernullpointer 5322 gold badges7 silver badges25 bronze badges 5
  • Is there a way so that my pre-built JAR becomes the generated JAR? I'm trying to make sense of that but failing, since if you want the result to be what you have already, why are you not just running what you have already? – g00se Commented Jan 2 at 13:49
  • Why do you use the maven-assembly-plugin to create a jar? please show a full project example setup (github or alike?) – khmarbaise Commented Jan 2 at 15:20
  • @g00se I have it as a JAR in the code repository. Currently Ant handles it so that other modules can use it. In Maven, it needs to be a published artifact for other modules to use it, so that's what I am trying to do. Does that make sense or am I way off base here? – nullpointer Commented Jan 3 at 18:15
  • @khmarbaise This is pretty much the whole project other. Do you want to look at something specific? I just came across this plug-in that I thought would help achieve my goal. Should I try something else? – nullpointer Commented Jan 3 at 18:16
  • 1 Well then I shall just ignore the sentence I quoted and just say that, per the answer given, you should indeed use the maven-install plugin. Once you've installed your jar you can just forget about it – g00se Commented Jan 4 at 9:31
Add a comment  | 

1 Answer 1

Reset to default 1

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.
转载请注明原文地址:http://www.anycun.com/QandA/1746117688a91913.html