|
| 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