Java:使用Maven對普通java程序打包(可運行包 與 可引用包)


依開發環境需要,有時我們並不會運行到框架打包,這時就需要依靠Maven進行普通java程序的打包。

打包可運行jar包(不包括依賴的其他jar包)

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>org.example</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
<configuration> <archive> <manifest> <!--這里改成你的main方法類路徑--> <mainClass>com.dll.test.PlanMethod</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>

打包可運行jar包(包括依賴的其他jar包)

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>org.example</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!--這里改成你的main方法類路徑-->
                            <mainClass>com.dll.test.PlanMethod</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- 這里用於繼承合並 -->
                        <phase>package</phase> <!-- 綁定到打包階段 -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

打包可依賴jar包和可運行jar包(包括依賴的其他jar包

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>org.example</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!--這里改成你的main方法類路徑-->
                            <mainClass>com.dll.test.PlanMethod</mainClass>
                        </manifest>
                    </archive>
                    <!-- 配置生成的可運行jar名稱 -->
                    <descriptorRefs>
                        <descriptorRef>exec</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- 這里用於繼承合並 -->
                        <phase>package</phase> <!-- 綁定到打包階段 -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                    <!-- 在執行package動作的時候,自動打包 -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Java程序打包

如果大家是IDEA、Eclipse的話可運行clear、install打包,如果不是的可用命令打包

#運行maven命令打包程序(在pom.xml目錄下運行)
mvn package

 

文章轉載至:https://blog.csdn.net/strongyoung88/article/details/54097830


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM