Maven 打包可直接运行的 Java 程序

Maven 打包可直接运行的 Java 程序

1、在 pom 文件中配置 assembly 插件;

2、maven package 打包;

3、在 target 目录下生成 jar 文件(与普通 jar 包的区别是带有 with-dependencies):kafkaclient-1.0-SNAPSHOT-jar-with-dependencies.jar;

4、直接通过 java -jar kafkaclient-1.0-SNAPSHOT-jar-with-dependencies.jar 运行即可;

<?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>kafkaclient</groupId>
    <artifactId>kafkaclient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 项目所需要的依赖 -->
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.11</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

    <!-- 通过 assembly 插件打可运行的 jar 包 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.kafka.MsgProducer</mainClass>
                        </manifest>
                    </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>
        </plugins>
    </build>
</project>
i

发表评论

电子邮件地址不会被公开。 必填项已用*标注