adding jar libraries into jar file

2020-03-30 02:44发布

I am doing a maven project. Everything is fine when compiling and running my project in an idea, but whenever I create jar file, my external jar files in web/lib/ cannot be copied into the jar file. Why this occurs ? Can I insert my all files into the jar file ?

标签: java maven
3条回答
Fickle 薄情
2楼-- · 2020-03-30 03:06

Yes I found solution.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
 
        <finalName>HelloWorld</finalName>
 
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.gui.launcher.LauncherMain</mainClass>
            </manifest>
        </archive>
 
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
 
    <executions>
        <execution>
            <id>make-assembly</id>
            <!-- this is used for inheritance merges -->
            <phase>package</phase>
            <!-- append to the packaging phase. -->
            <goals>
                <goal>single</goal>
                <!-- goals == mojos -->
            </goals>
        </execution>
    </executions>
</plugin>
查看更多
何必那么认真
3楼-- · 2020-03-30 03:07

You can use the jar-with-dependencies descriptor of Maven Assembly Plugin to achieve this.

查看更多
贪生不怕死
4楼-- · 2020-03-30 03:12

You need to use Maven Assembly plugin something like this:

</project>
 ...
 <build>
      ...
      <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <archive>
                <manifest> <!-- requires for executable Jar -->
                  <mainClass>org.my.main.MainClass</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef> <!-- final Jar will have this text appended -->
              </descriptorRefs>
            </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- append to the packaging phase. -->
            <goals>
              <goal>single</goal> <!-- goals == mojos -->
            </goals>
          </execution>
        </executions>
      </plugin>
      ....
      </plugins>
  </build>
</project>
查看更多
登录 后发表回答