Skip to content

Commit

Permalink
Added run goal and updated README
Browse files Browse the repository at this point in the history
Refactored goals,
  - "run" will now run the server synchronously
  - "start" will run asynchronously
Updated README with better usage examples
  • Loading branch information
garrettheel committed Mar 23, 2013
1 parent e24b2e2 commit 01aeb9b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 34 deletions.
55 changes: 47 additions & 8 deletions README.md
@@ -1,12 +1,14 @@
# Overview
Overview
=============

This is a simple Maven plugin for managing a Moco server.
This is a simple Maven plugin for managing a [Moco] (https://github.com/dreamhead/moco) server. It can be used to simplify manual running of the server or to automatically launch the server during some part of the maven lifecycle (such as `integration-test`).

# Usage
Usage
=============

First, add the plugin to your `pom.xml`.
To get started, add the plugin to your `pom.xml`.

```
```xml
<plugin>
<groupId>moco-maven-plugin</groupId>
<artifactId>moco-maven-plugin</artifactId>
Expand All @@ -19,6 +21,43 @@ First, add the plugin to your `pom.xml`.
</plugin>
```

You can then execute the start or stop goals like so,
* `mvn moco-maven-plugin:moco-maven-plugin:start`
* `mvn moco-maven-plugin:moco-maven-plugin:stop`
## Running manually

Starting the server manually is easy! Simply run the following command:

```
mvn com.garrettheel:moco-maven-plugin:run
```

This will run the the server indefinitely until the process is terminated.

## Running during the maven lifecycle

You can also configure maven to start and stop the Moco server during the build lifecycle. For example, the following configuration would support using the server for integration testing:

```xml
<plugin>
<groupId>com.garrettheel</groupId>
<!-- ... -->
<configuration>
<port>8081</port>
<configFile>config.json</configFile>
</configuration>
<executions>
<execution>
<id>start-moco</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-moco</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
```
57 changes: 57 additions & 0 deletions src/main/java/com/garrettheel/moco/MocoRunMojo.java
@@ -0,0 +1,57 @@
package com.garrettheel.moco;

import com.github.dreamhead.moco.bootstrap.Main;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;

/**
* Runs a Moco server with a config file and port number. Note that the server will run
* synchronously and can be stopped by killing the process.
*/
@Mojo( name="run" )
public class MocoRunMojo extends AbstractMojo {

/**
* The file containing the JSON configuration.
*/
@Parameter(required = true)
private File configFile;

/**
* The port to start the server on.
*/
@Parameter(required = true)
private Integer port;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkParams();

Main.main(new String[]{
"-p", String.format("%d", port), // port
configFile.getAbsolutePath() // config file
});

getLog().info("Started Moco server on port " + port);

try {
Thread.currentThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void checkParams() throws MojoExecutionException {
if (!configFile.exists()) {
throw new MojoExecutionException("Moco config file does not exist.");
}
if (port == null || port < 1) {
throw new MojoExecutionException("Invalid port number specified.");
}
}
}
46 changes: 23 additions & 23 deletions src/main/java/com/garrettheel/moco/MocoStartMojo.java
@@ -1,17 +1,18 @@
package com.garrettheel.moco;

import com.github.dreamhead.moco.bootstrap.Main;
import com.github.dreamhead.moco.runner.DynamicRunner;
import com.github.dreamhead.moco.runner.Runner;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;

/**
* Starts a Moco server with a config file and port number.
* Starts a Moco server with a config file and port number. Note that the server will
* be run asynchronously and can be stopped using the `stop` goal.
*/
@Mojo(name = "start")
public class MocoStartMojo extends AbstractMojo {
Expand All @@ -31,38 +32,37 @@ public class MocoStartMojo extends AbstractMojo {
/**
* The port to stop the server on (optional).
*/
@Parameter(required = false)
@Parameter(required = false, defaultValue = "8082")
private Integer stopPort;

public void execute() throws MojoExecutionException, MojoFailureException {
if (!configFile.exists()) {
throw new MojoExecutionException("Moco config file does not exist.");
}
checkParams();

Main.main(new String[] {
"-p", String.format("%d", port), // port
configFile.getAbsolutePath() // config file
});
Runner runner = new DynamicRunner(configFile.getAbsolutePath(), port);
runner.run();

getLog().info("Started Moco server on port " + port);

if (stopPort != null) {
waitForStopSignal();
} else {
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
waitForStopSignal(runner);

}

private void checkParams() throws MojoExecutionException {
if (!configFile.exists()) {
throw new MojoExecutionException("Moco config file does not exist.");
}
if (port == null || port < 1) {
throw new MojoExecutionException("Invalid port number specified.");
}
if (stopPort == null || stopPort < 1) {
throw new MojoExecutionException("Invalid stop port number specified.");
}
}

private void waitForStopSignal() {
Thread monitor = null;
private void waitForStopSignal(Runner runner) {
try {
monitor = new StopMonitor(stopPort, StopMonitor.MONITOR_KEY);
Thread monitor = new StopMonitor(runner, stopPort, StopMonitor.MONITOR_KEY);
monitor.start();
monitor.join();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/garrettheel/moco/MocoStopMojo.java
Expand Up @@ -20,7 +20,7 @@ public class MocoStopMojo extends AbstractMojo {
/**
* The port to stop the server on.
*/
@Parameter(required = false)
@Parameter(required = false, defaultValue = "8082")
private Integer stopPort;


Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/garrettheel/moco/StopMonitor.java
@@ -1,5 +1,7 @@
package com.garrettheel.moco;

import com.github.dreamhead.moco.runner.Runner;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
Expand All @@ -13,9 +15,11 @@ public class StopMonitor extends Thread {

private String key;
private ServerSocket serverSocket;
private Runner runner;

public StopMonitor(int port, String key) throws IOException {
public StopMonitor(Runner runner, int port, String key) throws IOException {
this.key = key;
this.runner = runner;

this.setDaemon(true);
this.setName("MocoStopPluginMonitor");
Expand Down Expand Up @@ -44,7 +48,8 @@ public void run() {
e.printStackTrace();
}

System.exit(0);
runner.stop();
break;

} catch (IOException e) {
e.printStackTrace();
Expand Down

0 comments on commit 01aeb9b

Please sign in to comment.