add it for run mojo #1638

Signed-off-by: olivier lamy <olamy@webtide.com>
This commit is contained in:
olivier lamy 2017-06-22 16:24:13 +10:00
parent 064feaccc7
commit a275032915
14 changed files with 481 additions and 18 deletions

View File

@ -13,6 +13,7 @@
<mavenVersion>3.0.3</mavenVersion>
<pluginToolsVersion>3.4</pluginToolsVersion>
<bundle-symbolic-name>${project.groupId}.maven.plugin</bundle-symbolic-name>
<it.debug>false</it.debug>
</properties>
<build>
<plugins>
@ -153,6 +154,12 @@
<artifactId>javax.transaction-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
@ -177,4 +184,44 @@
</plugin>
</plugins>
</reporting>
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<debug>${it.debug}</debug>
<addTestClassPath>true</addTestClassPath>
<projectsDirectory>src/it</projectsDirectory>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<preBuildHookScript>setup</preBuildHookScript>
<postBuildHookScript>verify</postBuildHookScript>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<settingsFile>src/it/settings.xml</settingsFile>
<goals>
<goal>clean</goal>
</goals>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1 @@
invoker.goals = test

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.webtide.jetty.its.jetty-run-mojo-it</groupId>
<artifactId>jetty-simple-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>jetty-simple-base</artifactId>
<packaging>jar</packaging>
<name>Jetty :: Simple :: Base</name>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-perf-helper</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,44 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package com.webtide.jetty.its.jetty_run_mojo_it;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
*/
@WebServlet("/hello")
public class HelloServlet
extends HttpServlet
{
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
String who = req.getParameter( "name" );
resp.getWriter().write( "hello " + (who == null ? "unknown" : who) );
}
}

View File

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.webtide.jetty.its.jetty-run-mojo-it</groupId>
<artifactId>jetty-simple-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>jetty-simple-webapp</artifactId>
<packaging>war</packaging>
<name>Jetty :: Simple :: Webapp</name>
<dependencies>
<dependency>
<groupId>com.webtide.jetty.its.jetty-run-mojo-it</groupId>
<artifactId>jetty-simple-base</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>@project.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin-version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<executions>
<execution>
<id>start-jetty</id>
<phase>test-compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<nonBlocking>true</nonBlocking>
<propertiesPortFilePath>${project.build.directory}/jetty.properties</propertiesPortFilePath>
<httpConnector>
<port>0</port>
</httpConnector>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Jetty Simple Webapp run-mojo-it</display-name>
</web-app>

View File

@ -0,0 +1,66 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package com.webtide.jetty.its.jetty_run_mojo_it;
import org.eclipse.jetty.client.HttpClient;
import org.junit.Assert;
import org.junit.Test;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
/**
*
*/
public class TestHelloServlet
{
@Test
public void hello_servlet()
throws Exception
{
Path path = Paths.get( "target/jetty.properties" );
int port;
try (InputStream inputStream = Files.newInputStream( path ))
{
Properties properties = new Properties();
properties.load( inputStream );
port = Integer.parseInt( properties.getProperty( "port" ) );
}
HttpClient httpClient = new HttpClient();
try
{
httpClient.start();
String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString();
System.out.println( "httpResponse:" + response );
Assert.assertEquals( "hello beer", response.trim() );
}
finally
{
httpClient.stop();
}
}
}

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webtide.jetty.its.jetty-run-mojo-it</groupId>
<artifactId>jetty-simple-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Jetty :: Simple</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven-war-plugin-version>3.0.0</maven-war-plugin-version>
</properties>
<modules>
<module>jetty-simple-base</module>
<module>jetty-simple-webapp</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.webtide.jetty.its.jetty-run-mojo-it</groupId>
<artifactId>jetty-simple-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<profiles>
<profile>
<id>it-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>

View File

@ -21,9 +21,13 @@ package org.eclipse.jetty.maven.plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
@ -36,11 +40,14 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
@ -274,7 +281,12 @@ public abstract class AbstractJettyMojo extends AbstractMojo
protected ServerSupport serverSupport;
/**
* Will dump port in a properties file with key port.
* If empty no file generated
* @parameter
*/
protected String propertiesPortFilePath;
/**
* <p>
@ -287,8 +299,9 @@ public abstract class AbstractJettyMojo extends AbstractMojo
* If true, the server will not block the execution of subsequent code. This
* is the behaviour of the jetty:start and default behaviour of the jetty:deploy goals.
* </p>
* @parameter default-value="false"
*/
protected boolean nonblocking = false;
protected boolean nonBlocking = false;
public abstract void restartWebApp(boolean reconfigureScanner) throws Exception;
@ -429,11 +442,13 @@ public abstract class AbstractJettyMojo extends AbstractMojo
// if a <httpConnector> was specified in the pom, use it
if (httpConnector != null)
{
// check that its port was set
if (httpConnector.getPort() <= 0)
if (httpConnector.getPort() < 0)
{
//use any jetty.http.port settings provided
String tmp = System.getProperty(MavenServerConnector.PORT_SYSPROPERTY, System.getProperty("jetty.port", MavenServerConnector.DEFAULT_PORT_STR));
String tmp = System.getProperty(MavenServerConnector.PORT_SYSPROPERTY,
System.getProperty("jetty.port", MavenServerConnector.DEFAULT_PORT_STR));
httpConnector.setPort(Integer.parseInt(tmp.trim()));
}
httpConnector.setServer(server);
@ -459,7 +474,25 @@ public abstract class AbstractJettyMojo extends AbstractMojo
// start Jetty
this.server.start();
if (httpConnector != null)
{
int port = httpConnector.getLocalPort();
getLog().info( "Started Jetty Server on port: " + port );
if (propertiesPortFilePath != null)
{
Path propertiesPath = Paths.get( propertiesPortFilePath);
Files.deleteIfExists(propertiesPath);
try(OutputStream outputStream = Files.newOutputStream( propertiesPath ))
{
Properties properties = new Properties( );
properties.put( "port", Integer.toString( port ) );
properties.store( outputStream, "Eclipse Jetty Maven Plugin port used" );
}
}
} else
{
getLog().info( "Started Jetty Server" );
}
if ( dumpOnStart )
{
@ -478,10 +511,11 @@ public abstract class AbstractJettyMojo extends AbstractMojo
startConsoleScanner();
// keep the thread going if not in daemon mode
if (!nonblocking )
if (!nonBlocking )
{
server.join();
}
}
catch (Exception e)
{
@ -489,7 +523,7 @@ public abstract class AbstractJettyMojo extends AbstractMojo
}
finally
{
if (!nonblocking )
if (!nonBlocking )
{
getLog().info("Jetty server exiting.");
}
@ -504,7 +538,7 @@ public abstract class AbstractJettyMojo extends AbstractMojo
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
monitor.setPort(stopPort);
monitor.setKey(stopKey);
monitor.setExitVm(!nonblocking);
monitor.setExitVm(!nonBlocking );
}
}

View File

@ -61,7 +61,7 @@ public class JettyDeployWar extends JettyRunWarMojo
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
nonblocking = daemon;
nonBlocking = daemon;
super.execute();
}
@ -72,7 +72,7 @@ public class JettyDeployWar extends JettyRunWarMojo
{
super.finishConfigurationBeforeStart();
//only stop the server at shutdown if we are blocking
server.setStopAtShutdown(!nonblocking);
server.setStopAtShutdown(!nonBlocking );
}
}

View File

@ -222,7 +222,7 @@ public class JettyRunMojo extends AbstractJettyMojo
{
getLog().info("Reload Mechanic: " + reload );
}
getLog().info( "nonBlocking:" + nonBlocking );
// check the classes to form a classpath with
try
@ -566,7 +566,7 @@ public class JettyRunMojo extends AbstractJettyMojo
*/
private List<File> getDependencyFiles()
{
List<File> dependencyFiles = new ArrayList<File>();
List<File> dependencyFiles = new ArrayList<>();
for ( Iterator<Artifact> iter = projectArtifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = iter.next();
@ -596,7 +596,7 @@ public class JettyRunMojo extends AbstractJettyMojo
private List<File> getDependencyProjects()
{
List<File> dependencyFiles = new ArrayList<File>();
List<File> dependencyFiles = new ArrayList<>();
for ( Iterator<Artifact> iter = projectArtifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = iter.next();
@ -656,10 +656,10 @@ public class JettyRunMojo extends AbstractJettyMojo
if (warArtifacts != null)
return warArtifacts;
warArtifacts = new ArrayList<Artifact>();
warArtifacts = new ArrayList<>();
for ( Iterator<Artifact> iter = projectArtifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
Artifact artifact = iter.next();
if (artifact.getType().equals("war") || artifact.getType().equals("zip"))
{
try

View File

@ -44,7 +44,7 @@ public class JettyStartMojo extends JettyRunMojo
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
nonblocking = true; //ensure that starting jetty won't hold up the thread
nonBlocking = true; //ensure that starting jetty won't hold up the thread
super.execute();
}

View File

@ -271,6 +271,11 @@ public class MavenServerConnector extends ContainerLifeCycle implements Connecto
return this.name;
}
public int getLocalPort()
{
return this.delegate.getLocalPort();
}
private void checkDelegate() throws IllegalStateException
{
if (this.delegate == null)