Solon Web 开发,三、打包与运行

1、程序打包

在 pom.xml 中配置打包的相关插件。Solon 的项目必须开启编译参数:-parameters

<!-- 建议的配置:指定相关编码为 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>

        <!-- 配置编译插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <compilerArgument>-parameters</compilerArgument> 
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <!-- 配置打包插件(设置主类,并打包成胖包) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <finalName>${project.name}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>webapp.DemoApp</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、服务端口控制(此处再提一下)

在应用主配置文件里指定:

server.port: 8081

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

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

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

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

3、运行

终端运行:java -jar DemoApp.jar 即可启动

或者

终端运行:java -jar DemoApp.jar -server.prot=9091 启动时控制端口

4、尝试用 systemctl service 模式管理服务(仅供参考,linux 可用)

添加 systemctl service 配置,waterapi为例:/etc/systemd/system/waterapi.service

[Unit]
Description=waterapi
After=syslog.target

[Service]
ExecStart=/usr/bin/java -jar /data/sss/water/waterapi.jar
SuccessExitStatus=143
SuccessExitStatus=143
Restart=on-failure

[Install]
WantedBy=multi-user.target

通过 systemctl 指令操控服务:

systemctl enable waterapi

systemctl restart waterapi

systemctl stop waterapi

配置好后之后,服务更新的简单操作:

  1. 更新 jar 文件
  2. 运行 systemctl restart waterapi 即可

5、尝试用 docker 模式管理服务(仅供参考)

本地需要安装 Docker Desktop。然后在 pom.xml 增加镜像打包的 maven 插件配置。

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <imageName>${project.artifactId}</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
            <imageTag>latest</imageTag>
        </imageTags>
        <baseImage>adoptopenjdk/openjdk11</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar", "--server.port=8080","--drift=1"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

运行插件的:"docker:build" 命令之后,就会进入本地仓库了。如何发布到中央仓库或别的远程仓库,可以网上搜下。

#第一次运行
docker run -d -p 8080:8080 waterapi

#之后
docker restart waterapi

docker stop waterapi
posted @ 2022-01-19 08:12  带刺的坐椅  阅读(150)  评论(0编辑  收藏  举报