Solon

使用 maven-assembly-plugin 打胖包 [推荐1]

</> markdown

1、程序打包

在 pom.xml 中配置打包的相关插件。Solon 的项目需要开启编译参数:-parameters (引入 solon-parent 会自动开启)

  • 使用 solon-parent
<parent>
    <groupId>org.noear</groupId>
    <artifactId>solon-parent</artifactId>
    <version>${solon.version}</version>
    <relativePath />
</parent>

<packaging>jar</packaging>

<properties>
  <java.version>11</java.version>
</properties>

<!-- 主菜是这里: -->
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <!-- 配置打包插件(设置主类,并打包成胖包) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <!-- 此处,要改成自己的程序入口(即 main 函数类) -->
                    <manifest>
                        <mainClass>com.example.demo.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • 没有使用 solon-parent
<packaging>jar</packaging>

<!-- 建议的配置:指定相关编码为 UTF-8 -->
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  <java.version>11</java.version>
</properties>

<!-- 主菜是这里: -->
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <compilerArgument>-parameters</compilerArgument>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <!-- 配置打包插件(设置主类,并打包成胖包) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <finalName>${project.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <!-- 此处,要改成自己的程序入口(即 main 函数类) -->
                    <manifest>
                        <mainClass>com.example.demo.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

使用工具,或运行 maven 的 package 指令完成打包(IDEA的右侧边界面,也有这个菜单)

2、服务端口控制(此处再提一下)

在应用主配置文件里指定(即 app.yml 文件):

server.port: 8081

可以在运行时指定系统属性(优先级高):

java -Dserver.port=9091 -jar DemoApp.jar

还可以,在运行时通过启动参数指定(优先级更高):

java -jar DemoApp.jar -server.port=9091

3、运行(通过终端运行)

java -jar DemoApp.jar

#或者(运行时指定端口)
java -jar DemoApp.jar -server.port=9091

#或者(运行时指定端口和jvm编码)
java -Dfile.encoding=utf-8 -jar DemoApp.jar -server.port=9091