用于在Linux上创建Java守护程序服务的工具

时间:2022-01-29 12:02:21

What is the best way to create a java application that can be run using ‘service’ on Linux? I was going to use the JSW available here, but cannot use the licence on that (licence is either GPL or it costs money as far as I can tell). I’d need an apache style licence.

创建可以在Linux上使用“服务”运行的Java应用程序的最佳方法是什么?我打算使用这里提供的JSW,但不能使用许可证(许可证是GPL,或者据我所知,它需要花钱)。我需要一个apache风格的许可证。

I’m using maven to build, so it would be great if it was possible to create the service using a maven plugin, but any other suggestions would be great.

我正在使用maven进行构建,所以如果可以使用maven插件创建服务会很棒,但任何其他建议都会很棒。

I've seen Apache Commons Daemon, is there a maven plugin for this? Documentation seems sparse, so a working example of this would be good...

我见过Apache Commons Daemon,有没有maven插件呢?文档似乎很少,所以这个例子很好......

Thanks

谢谢

3 个解决方案

#1


20  

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.

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

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

查看Init Scripts以及对服务,运行级别和rc.d脚本的介绍

#2


13  

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.

据我所知,Apache Daemon或Akuma都没有Maven插件。虽然您可以尝试使用maven-exec-plugin在Maven构建中调用它们。


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.

至于贵公司对使用GPL许可产品的保留意见,值得仔细阅读使用的含义。它不像公司所担心的那样恶毒。这是对GPL的解释。它当然在法律上没有任何重要性(可能不正确或先例支持,我不是律师),但可能足以让您开始与您的法律人员进行对话。

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.

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


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.

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

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:

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

  <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

#3


3  

You can look at the following projects.

您可以查看以下项目。

#1


20  

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.

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

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

查看Init Scripts以及对服务,运行级别和rc.d脚本的介绍

#2


13  

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.

据我所知,Apache Daemon或Akuma都没有Maven插件。虽然您可以尝试使用maven-exec-plugin在Maven构建中调用它们。


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.

至于贵公司对使用GPL许可产品的保留意见,值得仔细阅读使用的含义。它不像公司所担心的那样恶毒。这是对GPL的解释。它当然在法律上没有任何重要性(可能不正确或先例支持,我不是律师),但可能足以让您开始与您的法律人员进行对话。

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.

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


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.

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

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:

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

  <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

#3


3  

You can look at the following projects.

您可以查看以下项目。