|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.maven; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URL; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.maven.model.Resource; |
| 28 | +import org.apache.maven.plugin.MojoExecutionException; |
| 29 | +import org.apache.maven.plugin.MojoFailureException; |
| 30 | +import org.apache.maven.plugins.annotations.Parameter; |
| 31 | +import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; |
| 32 | + |
| 33 | +import org.springframework.boot.loader.tools.FileUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * Base class to run a spring application. |
| 37 | + * |
| 38 | + * @author Phillip Webb |
| 39 | + * @author Stephane Nicoll |
| 40 | + * @author David Liu |
| 41 | + * @author Daniel Young |
| 42 | + * @author Dmytro Nosan |
| 43 | + * @since 6.0.0 |
| 44 | + * @see RunMojo |
| 45 | + * @see StartMojo |
| 46 | + */ |
| 47 | +public abstract class AbstractApplicationRunMojo extends AbstractRunMojo { |
| 48 | + |
| 49 | + /** |
| 50 | + * Add maven resources to the classpath directly, this allows live in-place editing of |
| 51 | + * resources. Duplicate resources are removed from {@code target/classes} to prevent |
| 52 | + * them to appear twice if {@code ClassLoader.getResources()} is called. Please |
| 53 | + * consider adding {@code spring-boot-devtools} to your project instead as it provides |
| 54 | + * this feature and many more. |
| 55 | + * @since 1.0.0 |
| 56 | + */ |
| 57 | + @Parameter(property = "spring-boot.run.addResources", defaultValue = "false") |
| 58 | + private boolean addResources = false; |
| 59 | + |
| 60 | + /** |
| 61 | + * Path to agent jars. NOTE: a forked process is required to use this feature. |
| 62 | + * @since 2.2.0 |
| 63 | + */ |
| 64 | + @Parameter(property = "spring-boot.run.agents") |
| 65 | + private File[] agents; |
| 66 | + |
| 67 | + /** |
| 68 | + * Flag to say that the agent requires -noverify. |
| 69 | + * @since 1.0.0 |
| 70 | + */ |
| 71 | + @Parameter(property = "spring-boot.run.noverify") |
| 72 | + private boolean noverify = false; |
| 73 | + |
| 74 | + /** |
| 75 | + * Flag to include the test classpath when running. |
| 76 | + * @since 1.3.0 |
| 77 | + */ |
| 78 | + @Parameter(property = "spring-boot.run.useTestClasspath", defaultValue = "false") |
| 79 | + private Boolean useTestClasspath; |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void run(File workingDirectory, String startClassName, Map<String, String> environmentVariables) |
| 83 | + throws MojoExecutionException, MojoFailureException { |
| 84 | + List<String> args = new ArrayList<>(); |
| 85 | + addAgents(args); |
| 86 | + addJvmArgs(args); |
| 87 | + addClasspath(args); |
| 88 | + args.add(startClassName); |
| 89 | + addArgs(args); |
| 90 | + run(workingDirectory, args, environmentVariables); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Run with a forked VM, using the specified command line arguments. |
| 95 | + * @param workingDirectory the working directory of the forked JVM |
| 96 | + * @param args the arguments (JVM arguments and application arguments) |
| 97 | + * @param environmentVariables the environment variables |
| 98 | + * @throws MojoExecutionException in case of MOJO execution errors |
| 99 | + * @throws MojoFailureException in case of MOJO failures |
| 100 | + */ |
| 101 | + protected abstract void run(File workingDirectory, List<String> args, Map<String, String> environmentVariables) |
| 102 | + throws MojoExecutionException, MojoFailureException; |
| 103 | + |
| 104 | + @Override |
| 105 | + protected URL[] getClassPathUrls() throws MojoExecutionException { |
| 106 | + try { |
| 107 | + List<URL> urls = new ArrayList<>(); |
| 108 | + addUserDefinedDirectories(urls); |
| 109 | + addResources(urls); |
| 110 | + addProjectClasses(urls); |
| 111 | + FilterArtifacts filters = (this.useTestClasspath ? getFilters() : getFilters(new TestArtifactFilter())); |
| 112 | + addDependencies(urls, filters); |
| 113 | + return urls.toArray(new URL[0]); |
| 114 | + } |
| 115 | + catch (IOException ex) { |
| 116 | + throw new MojoExecutionException("Unable to build classpath", ex); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void addAgents(List<String> args) { |
| 121 | + if (this.agents != null) { |
| 122 | + if (getLog().isInfoEnabled()) { |
| 123 | + getLog().info("Attaching agents: " + Arrays.asList(this.agents)); |
| 124 | + } |
| 125 | + for (File agent : this.agents) { |
| 126 | + args.add("-javaagent:" + agent); |
| 127 | + } |
| 128 | + } |
| 129 | + if (this.noverify) { |
| 130 | + args.add("-noverify"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void addResources(List<URL> urls) throws IOException { |
| 135 | + if (this.addResources) { |
| 136 | + for (Resource resource : this.project.getResources()) { |
| 137 | + File directory = new File(resource.getDirectory()); |
| 138 | + urls.add(directory.toURI().toURL()); |
| 139 | + FileUtils.removeDuplicatesFromOutputDirectory(this.classesDirectory, directory); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments