Maven 打包可执行 jar

pom.xml 配置如下:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>assemble-executable-jar</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <name>myjar</name>
    <url>http://maven.apache.org</url>
    
    <build>
        <plugins>
            <!-- 打包用的plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                
                <!-- 配置入口函数及配置文件 -->
                <configuration>
                    <archive>
                        <!-- 配置入口静态函数 -->
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.test.Main</mainClass>
                        </manifest>

                        <!-- 指定配置文件位置 
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                        -->
                    </archive>

                    <!-- 打包的包名描述,可省略 -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>

                <!-- 执行打包的方法 -->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 配置编译器版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

        <!-- 指定程序所需配置文件打包后的相对位置 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>dbconfig.properties</include>
                </includes>
                <filtering>false</filtering>
                <targetPath>${project.build.directory}/config</targetPath>
            </resource>
        </resources>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.9.1</version>
        </dependency>
        
        <!-- more dependencies...  -->

    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

执行 jar 的 java 命令:

java -jar myjar.jar    # 后面可输入参数,供程序使用

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值