将jar库添加到jar文件中

时间:2011-02-15 05:54:04

标签: java maven

我正在做一个maven项目。在一个想法中编译和运行我的项目时一切都很好,但每当我创建jar文件时,web / lib /中的外部jar文件都无法复制到jar文件中。为什么会这样?我可以将所有文件都插入到jar文件中吗?

3 个答案:

答案 0 :(得分:2)

您可以使用jar-with-dependenciesMaven Assembly Plugin描述符来实现此目的。

答案 1 :(得分:1)

您需要使用Maven Assembly plugin这样的内容:

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

答案 2 :(得分:0)

是的我找到了解决方案。

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