此方案，依赖包的 jar 会移到外部的 lib 目录。<mark>此方案，为用户（.77）分享</mark>

提醒：

* 建议开启编译参数：-parameters （使用 solon-parent 作为 parent 时，会自动开启），否则方法的参数名会变成 arg0, arg1, arg2 之类的

### 1、程序打包

在 pom.xml 中配置打包的相关插件。


* 使用 solon-parent 作为 parent


```xml
<!-- 注意：引入 solon-parent，可省去很多细节配置 -->
<parent>
    <groupId>org.noear</groupId>
    <artifactId>solon-parent</artifactId>
    <version>${solon.version}</version>
    <relativePath />
</parent>

<packaging>jar</packaging>

<!-- 建议的配置：指定 Java 版本 -->
<properties>
    <java.version>11</java.version>
</properties>

<!-- 主菜是这里： -->
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <!-- 添加依赖输出控制 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.4.0</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 配置打包插件（设置主类，关联 lib 目录） -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <!-- 指定classpath的前缀 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!-- 指定主类的类名 -->
                        <mainClass>com.example.demo.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
```


* 没有使用 solon-parent

```xml
<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>
</properties>

<!-- 主菜是这里： -->
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
         <!-- 如果（使用 solon-parent 作为 parent ），这块则不用 -->
        <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-dependency-plugin</artifactId>
            <version>3.4.0</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 配置打包插件（设置主类，关联 lib 目录） -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <!-- 指定classpath的前缀 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!-- 指定主类的类名 -->
                        <mainClass>com.example.demo.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
```

使用工具，或运行 maven 的 package 指令完成打包（IDEA的右侧边界面，也有这个菜单）。输出工件为：

* DemoApp.jar
* lib/


### 2、服务端口控制（此处再提一下）

在应用主配置文件里指定（即 app.yml 文件）：

```yml
server.port: 8081
```


可以在运行时指定系统属性（优先级高）：

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


还可以，在运行时通过启动参数指定（优先级更高）：

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

### 3、运行（通过终端运行）

```shell
java -jar DemoApp.jar

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

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



