سؤال

How to quickly generate (possibly without coding anything) a System V init script to start and stop a service for a Jetty instance started using mvn jetty:run?

هل كانت مفيدة؟

المحلول

I'll preface this by saying that I have searched high and low and ended up having to craft my own solutions to this problem. Currently, I recommend using Apache Commons Daemon (used by Tomcat) as it provides the best daemon behavior of the things I've tried and is reasonably straightforward to write an init script for. However, that solution doesn't really meet your criteria of being quick to implement.

Instead, for a faster solution which requires no code changes, you can use appassembler-maven-plugin. Here is a sample pom based on a working configuration for a real product.

The configuration below is built for unix sysadmins. Here are some things to note about it:

  • It starts jetty using the xml-based entry point for jetty, org.eclipse.jetty.xml.XmlConfiguration. I think this is roughly equivalent to jetty:run.
  • /etc/my-jetty-server in the classpath so that configuration files such as jetty.xml or web.xml can be placed there.
  • jsw's startup logs are written to /var/log/my-jetty-server.

In addition, you'll probably want to package the resulting program and your dependencies so that you can install your product and start it with "service my-jetty-server start" or your operating system's equivalent. You can do that using the maven-assembly-plugin but that's somewhat out of the scope of this question.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>generate-jsw-scripts</id>
        <phase>package</phase>
        <goals>
          <goal>generate-daemons</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
      <target>${project.build.directory}</target>

      <daemons>
        <daemon>
          <id>my-jetty-server</id>
          <mainClass>org.eclipse.jetty.xml.XmlConfiguration</mainClass>

          <generatorConfigurations>
            <generatorConfiguration>
              <generator>jsw</generator>
              <includes>
                <include>linux-x86-32</include>
                <include>linux-x86-64</include>
              </includes>
              <configuration>
                <property>
                  <name>configuration.directory.in.classpath.first</name>
                  <value>/etc/my-jetty-server</value>
                </property>
                <property>
                  <name>wrapper.logfile</name>
                  <value>/var/log/my-jetty-server/out.log</value>
                </property>
              </configuration>
            </generatorConfiguration>
          </generatorConfigurations>
          <platforms>
            <platform>jsw</platform>
          </platforms>
        </daemon>
      </daemons>
    </configuration>
  </plugin>

My example above uses version 1.3 of the plugin but 1.8 is the latest of this writing. I'd expect the latest version should work with this configuration but I'd rather post something that is dated but that I know works. The plugin's documentation has more examples here http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/usage_jsw.html.

نصائح أخرى

You have in Unix tools like Automake, Autovonf, libtools, make and nmake but to create a script for start and stop I know only this way: https://unix.stackexchange.com/a/20361

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top