Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

maven embed dependency artifact code in jar

I have a multi-module maven project. I have a main "base-code" module which creates a jar of all the compiled source code in my project.

I have another module, "executable", which creates an executable jar from the same source code. To avoid duplication I want to pull the classes in from the "base-code" module.

I thought that all I had to do was make the "base-code" module a dependency of the "executable" module to do this. But I just get an empty jar. What am I doing wrong? (my "executable" pom is below)

 <project>
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.myproject/groupId>
    <artifactId>myproject</artifactId>
    <version>1</version>
</parent>
<artifactId>executable</artifactId>
<packaging>jar</packaging>

<dependencies>
    <dependency>
    <groupId>com.myproject/groupId>
        <artifactId>code-base</artifactId>
        <version>1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <finalName>runnable</finalName>
                <archive>
                    <manifest>
                        <mainClass>com.myproject.Main</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are looking for is probably uber-jar: a single jar file with all embedded jar dependencies, the newly version of maven-assembly-plugin support this as one of the 4 pre-defined descriptor, check out here.

Try using maven-assembly-plugin replace your maven-jar-plugin like this:

<!-- Create single executable jar with all dependencies unpacked and embedded -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.myproject.Main</mainClass>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
        </manifest>
      </archive>
      <descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals><goal>single</goal></goals>
      </execution>
    </executions>
  </plugin>

Alternatively, you can also use maven-shade-plugin to do this.

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...