【问题标题】:Tool for creating a Java daemon service on Linux [closed]在 Linux 上创建 Java 守护程序服务的工具 [关闭]
【发布时间】:2010-11-21 15:04:35
【问题描述】:

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

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

我见过Apache Commons Daemon,有这个的maven插件吗?文档似乎很少,所以一个可行的例子会很好......

谢谢

【问题讨论】:

    标签: java linux maven-2 maven-plugin daemon


    【解决方案1】:

    据我所知,没有适用于 Apache Daemon 或 Akuma 的 Maven 插件。尽管您可以尝试使用 maven-exec-plugin 从 Maven 构建中调用它们。


    就您的公司对使用 GPL 许可产品的保留意见而言,值得一读使用的含义。它并不像企业担心的那样恶毒。这是interpretation of the GPL。当然,它在法律上没有任何分量(并且可能不正确或没有先例支持,我不是律师),但可能足以让您与您的法律人员开始对话。

    来自参考页面:

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


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

    这是一个示例配置,它将在 target/appassembler 文件夹中生成独立应用程序,并在 target/appassembler/jsw/myApp 目录中生成 JSW 包装文件。注意 assemble 目标绑定到集成测试阶段,以确保创建项目的 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
    

    【讨论】:

    • @Rich - 感谢您的回答。我最初做过这样的事情,但是 JSW 的许可证对我来说太严格了(好吧,我的雇主!)。顺便说一句,是否可以在具有多个模块的项目上从主项目 POM 配置 Appassembler?我能想到的唯一方法是创建一个新模块,该模块的具体工作是创建在所有其他模块打包后运行的服务脚本(在我的情况下是 RPM)......
    • 如果您在依赖所有其他模块的模块上运行 assemble 目标,则这些依赖项将打包在 appassembler 目录中。所以它可能不会在您的聚合器 pom 上运行,而是在具有“主”类的模块上运行
    • 此外,GPL 许可并不像企业所认为的那样严格。阅读以下内容:sitepoint.com/article/public-license-explained
    • 非常有帮助。虽然仅供参考,但有一个更新的示例说明如何在 pom 中设置 appassembler 以构建服务,可在此处获得:mojo.codehaus.org/appassembler/appassembler-maven-plugin/…。在我的 JSW 服务正确安装/运行之前,我必须遵循该示例。
    【解决方案2】:

    你可以看看以下项目。

    【讨论】:

      【解决方案3】:

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

      看看Init ScriptsAn introduction to services, runlevels, and rc.d scripts

      【讨论】:

        猜你喜欢
        • 2011-02-13
        • 2011-12-16
        • 2017-05-08
        • 2013-07-31
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 2013-07-20
        相关资源
        最近更新 更多