在 Linux 上创建 Java 守护进程服务的工具

发布于 2024-08-02 23:36:33 字数 1536 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩饰不了的爱 2024-08-09 23:36:33

Linux 上的服务只是启动后台进程的 shell 脚本。查看 /etc/init.d - 您可以在文本编辑器中打开这些文件。您所需要的只是一个 bash 脚本,它以适当的方式响应参数 startstop (例如 start 将启动您的服务并记录已知位置的进程 ID,stop 将使用您创建的文件中的 PID 终止该进程),然后将其放入 /etc/init.d 中。

查看 初始化脚本服务、运行级别、和 rc.d 脚本

Services on Linux are just shell scripts which start background processes. Have a look in /etc/init.d - you can open the files in a text editor. All you need is a bash script which responds to the parameters start and stop in an appropriate way (eg. start will start your service and record the process ID in a known location, stop will kill the process with the PID from the file you created), and then place it in /etc/init.d.

Have a look at Init Scripts and An introduction to services, runlevels, and rc.d scripts

一世旳自豪 2024-08-09 23:36:33

据我所知,Apache Daemon 或 Akuma 都没有 Maven 插件。尽管您可以尝试使用 maven-exec-plugin。


至于您的公司对使用 GPL 许可产品的保留意见,值得阅读一下使用的影响。它并不像企业担心的那么致命。这是GPL 的解释。当然,它在法律上没有任何影响力(并且可能不正确或没有先例支持,我不是律师),但可能足以让您与法律人员开始对话。

从引用的页面:

简单地将受版权保护的作品与另一作品相结合并不会产生衍生作品。原始受版权保护的作品必须以某种方式进行修改。由此产生的衍生作品本身必须“代表作者的原创作品”。因此,如果被许可人没有修改原始 GPL 许可的程序,而只是运行它,那么他就不是在创建衍生作品。


我认为 Appassembler Maven 插件 可以满足您的需求(尽管它确实创建 JSW 包装器)。它创建一个 shell 脚本(和一个 bat 文件),并将所有应用程序 jar 收集到一个目录中。可以选择配置以创建基于 JSW 的守护程序配置。

以下是一个示例配置,它将在 target/appassembler 文件夹中生成独立应用程序,并在 target/appassembler/jsw/myApp 目录中生成 JSW 包装器文件。请注意,组装目标绑定到集成测试阶段,以确保创建项目的 jar。要生成输出,请运行 mvn verify 或仅生成服务包装器,请运行 mvn package

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <name>myShellScript</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <!--collect all jars into the lib directory-->
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
      <execution>
        <id>generate-jsw-scripts</id>
        <phase>package</phase>
        <goals>
          <goal>generate-daemons</goal>
        </goals>
        <configuration>
          <!--declare the JSW config -->
          <daemons>
            <daemon>
              <id>myApp</id>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <commandLineArguments>
                <commandLineArgument>start</commandLineArgument>
              </commandLineArguments>
              <platforms>
                <platform>jsw</platform>
              </platforms>              
            </daemon>
          </daemons>
          <target>${project.build.directory}/appassembler</target>
        </configuration>
      </execution>
    </executions>
  </plugin>

作为参考,生成的文件如下:

myApp\bin\myApp
myApp\bin\myApp.bat
myApp\bin\wrapper-linux-x86-32
myApp\bin\wrapper-macosx-universal-32
myApp\bin\wrapper-solaris-x86-32
myApp\bin\wrapper-windows-x86-32.exe
myApp\conf\wrapper.conf
myApp\lib\libwrapper-linux-x86-32.so
myApp\lib\libwrapper-macosx-universal-32.jnilib
myApp\lib\libwrapper-solaris-x86-32.so
myApp\lib\wrapper-windows-x86-32.dll
myApp\lib\wrapper.jar

As far as I know there isn't a Maven plugin for either Apache Daemon or Akuma. Though you could attempt to invoke them from within a Maven build by using the maven-exec-plugin.


As far as your companies reservations about using GPL-licensed products, it's worth reading up on the implications of use. It is not as virulent as corporations fear. Here's an interpretation of the GPL. It of course doesn't carry any weight in law (and may not be correct or supported by precedent, I am not a lawyer), but might be sufficient to allow you to start a conversation with your legal people.

From the referenced page:

Simply combining a copyrighted work with another work does not create a derivative work. The original copyrighted work must be modified in some way. The resulting derivative work must itself "represent an original work of authorship." So if the licensee does not modify the original GPL-licensed program, but merely runs it, he is not creating a derivative work.


There is the Appassembler Maven plugin that I think does what you need (though it does create JSW wrappers). It creates a shell script (and a bat file), and collects all the application jars into a directory. It can optionally be configured to create JSW-based Daemon configurations.

Here is an example configuration that will generate the standalone application in the target/appassembler folder, and generate the JSW wrapper files in the target/appassembler/jsw/myApp directory. Note the assemble goal is bound to the integration-test phase to ensure the project's jar is created. To generate the output run mvn verify or to just generate the service wrappers run mvn package:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <name>myShellScript</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <!--collect all jars into the lib directory-->
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
      <execution>
        <id>generate-jsw-scripts</id>
        <phase>package</phase>
        <goals>
          <goal>generate-daemons</goal>
        </goals>
        <configuration>
          <!--declare the JSW config -->
          <daemons>
            <daemon>
              <id>myApp</id>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <commandLineArguments>
                <commandLineArgument>start</commandLineArgument>
              </commandLineArguments>
              <platforms>
                <platform>jsw</platform>
              </platforms>              
            </daemon>
          </daemons>
          <target>${project.build.directory}/appassembler</target>
        </configuration>
      </execution>
    </executions>
  </plugin>

For reference the generated files are as follows:

myApp\bin\myApp
myApp\bin\myApp.bat
myApp\bin\wrapper-linux-x86-32
myApp\bin\wrapper-macosx-universal-32
myApp\bin\wrapper-solaris-x86-32
myApp\bin\wrapper-windows-x86-32.exe
myApp\conf\wrapper.conf
myApp\lib\libwrapper-linux-x86-32.so
myApp\lib\libwrapper-macosx-universal-32.jnilib
myApp\lib\libwrapper-solaris-x86-32.so
myApp\lib\wrapper-windows-x86-32.dll
myApp\lib\wrapper.jar
极致的悲 2024-08-09 23:36:33

您可以看看以下项目。

You can look at the following projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文