美文网首页
3. maven打包可运行的jar

3. maven打包可运行的jar

作者: 写代码的向日葵 | 来源:发表于2022-03-30 16:46 被阅读0次
  1. 方式一:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <archive>
           <manifest>
             <addClasspath>true</addClasspath>
                <mainClass>ui.Main</mainClass>
               <classpathPrefix>lib</classpathPrefix>
             </manifest>
        </archive>
     <finalName>hellojava</finalName>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
        </execution>
    </executions>
</plugin>

  1. 方式二:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <!--主类名称-->
                <mainClass>ui.Main</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!--文件名称-->
        <finalName>hello</finalName>
        <!--是否拼接字符串-->
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

  1. 方式三:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>ui.Main</mainClass>
                    </transformer>
                </transformers>
                <finalName>HelloJava</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

  1. 方式四

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.6.3</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <mainClass>ui.Main</mainClass>
                <finalName>HelloFx</finalName>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </execution>
    </executions>
</plugin>

打包jar文件到本地maven仓库

mvn install:install-file -Dfile=jar的路径 -DgroupId=组织 -Dartifactid=库名 -Dversion=版本 -Dpackaging=jar

相关文章

网友评论

      本文标题:3. maven打包可运行的jar

      本文链接:https://www.haomeiwen.com/subject/zfkwjrtx.html