Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - POM: How to make the jetty port changeable so that it can be retrieved later?

I'm working on an integration test suite and I've got a question for you.

My parent pom defines the use of the jetty plugin with the goal: run-war. I need to make the port that jetty listens on changeable via the command-line. This can be achieved by passing -Djetty.port=8099 for example.

In the child project, I need to use this port number to configure the endpoint for some SOAP tests that I'll need to run on the service hosted by jetty.

If I use ${jetty.port} in my child pom in the end-point configuration this works fine IF and only IF I explicitly pass -Djetty.port when invoking maven.

In my child pom:


<endpoint>http://127.0.0.1:${jetty.port}/{artifactId}<endpoint>

I need jetty.port to be filled in with 8080 which is what jetty defaults to if -Djetty.port is not explicitly passed, and still catch any other port values if the command line argument is specified.

like image 551
John Avatar asked Jul 26 '10 11:07

John


2 Answers

Use the properties section, and add a jetty.port property with a default value:

<properties>
  <jetty.port>8080</jetty.port>
</properties>
like image 145
Robert Munteanu Avatar answered Oct 07 '22 01:10

Robert Munteanu


config maven jetty plugin:

    <plugins>

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1H.14.1</version>
            <configuration>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>8085</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
             </configuration>
        </plugin>
    </plugins>

If you want to use a newer version of jetty plugin, use the following configuration:

From http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html:

You could instead configure the connectors in a standard jetty xml config file and put its location into the jettyXml parameter. Note that since jetty-9.0 it is no longer possible to configure a https connector directly in the pom.xml: you need to use jetty xml config files to do it.
Something like:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.0.5.v20130815</version>
        <configuration>
            <jettyXml>src/main/resources/jetty.xml</jettyXml>
            <webApp>
                <contextPath>/yourCtxPath</contextPath>
            </webApp>
        </configuration>
    </plugin>

would do the trick, with jetty.xml file content:


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call id="httpsConnector" name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
          <Set name="host"><Property name="jetty.host" /></Set>
          <Set name="port"><Property name="jetty.port" default="8085" /></Set>
          <Set name="idleTimeout">30000</Set>
        </New>
    </Arg>
  </Call>

</Configure>

See the log after 'mvn jetty:run', at the end should show something like:
2013-09-05 09:49:05.047:INFO:oejs.ServerConnector:main: Started ServerConnector@a6e9cb4{HTTP/1.1}{0.0.0.0:8085}

You will need to use maven 3 and java 7 for this version of plugin.

like image 37
gamoz Avatar answered Oct 07 '22 01:10

gamoz