Skip to content

Commit b6da61a

Browse files
committed
launch external programs from: mvn jooby:run fix #40
1 parent f1369b3 commit b6da61a

8 files changed

Lines changed: 372 additions & 156 deletions

File tree

‎jooby-maven-plugin/pom.xml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
<description>Set of Maven plugins for Jooby</description>
1717

1818
<dependencies>
19-
<dependency>
20-
<groupId>org.codehaus.mojo</groupId>
21-
<artifactId>exec-maven-plugin</artifactId>
22-
</dependency>
23-
2419
<dependency>
2520
<groupId>org.apache.maven</groupId>
2621
<artifactId>maven-plugin-api</artifactId>
@@ -47,11 +42,6 @@
4742
<artifactId>plexus-utils</artifactId>
4843
</dependency>
4944

50-
<dependency>
51-
<groupId>org.twdata.maven</groupId>
52-
<artifactId>mojo-executor</artifactId>
53-
</dependency>
54-
5545
<!-- Test dependencies -->
5646
<dependency>
5747
<groupId>junit</groupId>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
public class Command {
27+
28+
private boolean wait = true;
29+
30+
private String name;
31+
32+
private List<String> arguments;
33+
34+
private File workdir;
35+
36+
private Object alias;
37+
38+
private Process process;
39+
40+
private ProcessOutput out;
41+
42+
private ProcessOutput err;
43+
44+
public Command(final String alias, final String name, final List<String> args) {
45+
this.alias = alias;
46+
this.name = name;
47+
this.arguments = new ArrayList<String>(args);
48+
}
49+
50+
public Command() {
51+
}
52+
53+
public File getWorkdir() {
54+
return workdir;
55+
}
56+
57+
public void setWorkdir(final File workdir) {
58+
if (this.workdir == null) {
59+
this.workdir = workdir;
60+
}
61+
}
62+
63+
public boolean isWait() {
64+
return wait;
65+
}
66+
67+
public void setWait(final boolean wait) {
68+
this.wait = wait;
69+
}
70+
71+
public String getName() {
72+
return name;
73+
}
74+
75+
public void setName(final String name) {
76+
this.name = name;
77+
}
78+
79+
public List<String> getArguments() {
80+
return arguments;
81+
}
82+
83+
public void setArguments(final List<String> arguments) {
84+
this.arguments = arguments;
85+
}
86+
87+
public void stop() throws InterruptedException {
88+
if (this.process != null) {
89+
this.out.stopIt();
90+
this.err.stopIt();
91+
this.process.destroy();
92+
this.process.waitFor();
93+
this.process = null;
94+
this.out = null;
95+
this.err = null;
96+
}
97+
}
98+
99+
public void execute() throws IOException, InterruptedException {
100+
List<String> cmd = new ArrayList<String>();
101+
cmd.add(name);
102+
cmd.addAll(arguments);
103+
process = new ProcessBuilder(cmd)
104+
.directory(workdir)
105+
.start();
106+
// info
107+
out = new ProcessOutput(process.getInputStream(), false);
108+
out.start();
109+
// err
110+
err = new ProcessOutput(process.getErrorStream(), true);
111+
err.start();
112+
if (wait) {
113+
process.waitFor();
114+
}
115+
116+
}
117+
118+
@Override
119+
public String toString() {
120+
StringBuilder str = new StringBuilder();
121+
if (alias == null) {
122+
str.append(name);
123+
for (String arg : arguments) {
124+
str.append(" ").append(arg);
125+
}
126+
} else {
127+
str.append(alias);
128+
}
129+
return str.toString();
130+
}
131+
132+
public String debug() {
133+
StringBuilder str = new StringBuilder();
134+
str.append(name);
135+
for (String arg : arguments) {
136+
str.append(" ").append(arg);
137+
}
138+
return str.toString();
139+
}
140+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby;
20+
21+
import java.io.File;
22+
import java.util.ArrayList;
23+
import java.util.LinkedHashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
import java.util.stream.Collectors;
27+
28+
import org.apache.maven.artifact.Artifact;
29+
import org.apache.maven.model.Resource;
30+
import org.apache.maven.plugin.AbstractMojo;
31+
import org.apache.maven.plugin.MojoExecutionException;
32+
import org.apache.maven.plugin.MojoFailureException;
33+
import org.apache.maven.plugin.logging.Log;
34+
import org.apache.maven.plugins.annotations.Component;
35+
import org.apache.maven.plugins.annotations.Execute;
36+
import org.apache.maven.plugins.annotations.LifecyclePhase;
37+
import org.apache.maven.plugins.annotations.Mojo;
38+
import org.apache.maven.plugins.annotations.Parameter;
39+
import org.apache.maven.plugins.annotations.ResolutionScope;
40+
import org.apache.maven.project.MavenProject;
41+
42+
@Mojo(name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
43+
@Execute(phase = LifecyclePhase.TEST_COMPILE)
44+
public class JoobyMojo extends AbstractMojo {
45+
46+
@Component
47+
private MavenProject mavenProject;
48+
49+
@Parameter(property = "main.class", defaultValue = "${application.class}")
50+
private String mainClass;
51+
52+
@Parameter(defaultValue = "${project.build.outputDirectory}")
53+
private String buildOutputDirectory;
54+
55+
@Parameter(property = "jooby.commands")
56+
private List<Command> commands;
57+
58+
@Parameter(property = "jooby.vmArgs")
59+
private List<String> vmArgs;
60+
61+
@Override
62+
public void execute() throws MojoExecutionException, MojoFailureException {
63+
Set<String> classpath = new LinkedHashSet<String>();
64+
65+
// public / config, etc..
66+
classpath.addAll(resources(mavenProject.getResources()));
67+
68+
// target/classes
69+
classpath.add(buildOutputDirectory);
70+
71+
// *.jar
72+
for (Object candidate : mavenProject.getArtifacts()) {
73+
Artifact artifact = (Artifact) candidate;
74+
classpath.add(artifact.getFile().toString());
75+
}
76+
String cp = classpath.stream().collect(Collectors.joining(File.pathSeparator));
77+
78+
// prepare commands
79+
List<Command> cmds = new ArrayList<Command>();
80+
if (commands != null && commands.size() > 0) {
81+
cmds.addAll(0, this.commands);
82+
}
83+
List<String> args = new ArrayList<String>();
84+
args.addAll(vmArgs(vmArgs));
85+
args.add("-cp");
86+
args.add(cp);
87+
args.add(mainClass);
88+
cmds.add(new Command(mainClass, "java", args));
89+
90+
for (Command cmd : cmds) {
91+
cmd.setWorkdir(mavenProject.getBasedir());
92+
getLog().debug("cmd: " + cmd.debug());
93+
}
94+
/**
95+
* Shutdown hook
96+
*/
97+
Runtime.getRuntime().addShutdownHook(shutdownHook(cmds, getLog()));
98+
99+
/**
100+
* Start process
101+
*/
102+
for (Command cmd : cmds) {
103+
try {
104+
getLog().debug("Starting process: " + cmd);
105+
cmd.execute();
106+
} catch (Exception ex) {
107+
throw new MojoFailureException("Execution of " + cmd + " resulted in error", ex);
108+
}
109+
}
110+
111+
}
112+
113+
private List<String> vmArgs(final List<String> vmArgs) {
114+
List<String> results = new ArrayList<String>();
115+
if (vmArgs != null) {
116+
results.addAll(vmArgs);
117+
}
118+
File[] logbackFiles = {localFile("config", "logback-test.xml"),
119+
localFile("config", "logback.xml") };
120+
for (File logback : logbackFiles) {
121+
if (logback.exists()) {
122+
results.add("-Dlogback.configurationFile=" + logback.getAbsolutePath());
123+
break;
124+
}
125+
}
126+
return results;
127+
}
128+
129+
private File localFile(final String... paths) {
130+
File result = mavenProject.getBasedir();
131+
for (String path : paths) {
132+
result = new File(result, path);
133+
}
134+
return result;
135+
}
136+
137+
private List<String> resources(final Iterable<Resource> resources) {
138+
List<String> result = new ArrayList<String>();
139+
for (Resource resource : resources) {
140+
String dir = resource.getDirectory();
141+
if (new File(dir).exists()) {
142+
result.add(dir);
143+
}
144+
}
145+
return result;
146+
}
147+
148+
private static Thread shutdownHook(final List<Command> cmds, final Log log) {
149+
return new Thread() {
150+
@Override
151+
public void run() {
152+
for (Command command : cmds) {
153+
try {
154+
log.info("stopping: " + command);
155+
command.stop();
156+
} catch (Exception ex) {
157+
log.error("Stopping process: " + command + " resulted in error", ex);
158+
}
159+
}
160+
}
161+
};
162+
}
163+
}

0 commit comments

Comments
 (0)