From c59f8c197e0e73b2374600c27b509c87e58198a7 Mon Sep 17 00:00:00 2001 From: ileler Date: Thu, 2 Sep 2021 23:56:32 +0800 Subject: [PATCH] Added goal [fmc], support to find main class and set to specified property --- .../boot/maven/AbstractRunMojo.java | 30 +----- .../boot/maven/FindMainClassMojo.java | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+), 29 deletions(-) create mode 100755 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FindMainClassMojo.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java index 83ec65ed16d6..2c97243dd93c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java @@ -44,7 +44,6 @@ import org.springframework.boot.loader.tools.FileUtils; import org.springframework.boot.loader.tools.JavaExecutable; -import org.springframework.boot.loader.tools.MainClassFinder; /** * Base class to run a spring application. @@ -58,9 +57,7 @@ * @see RunMojo * @see StartMojo */ -public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { - - private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication"; +public abstract class AbstractRunMojo extends FindMainClassMojo { /** * The Maven project. @@ -167,14 +164,6 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { @Parameter(property = "spring-boot.run.profiles") private String[] profiles; - /** - * The name of the main class. If not specified the first compiled class found that - * contains a 'main' method will be used. - * @since 1.0.0 - */ - @Parameter(property = "spring-boot.run.main-class") - private String mainClass; - /** * Additional directories besides the classes directory that should be added to the * classpath. @@ -420,23 +409,6 @@ private void addClasspath(List args) throws MojoExecutionException { } } - private String getStartClass() throws MojoExecutionException { - String mainClass = this.mainClass; - if (mainClass == null) { - try { - mainClass = MainClassFinder.findSingleMainClass(this.classesDirectory, - SPRING_BOOT_APPLICATION_CLASS_NAME); - } - catch (IOException ex) { - throw new MojoExecutionException(ex.getMessage(), ex); - } - } - if (mainClass == null) { - throw new MojoExecutionException("Unable to find a suitable main class, please add a 'mainClass' property"); - } - return mainClass; - } - protected URL[] getClassPathUrls() throws MojoExecutionException { try { List urls = new ArrayList<>(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FindMainClassMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FindMainClassMojo.java new file mode 100755 index 000000000000..694984141417 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FindMainClassMojo.java @@ -0,0 +1,99 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.springframework.boot.loader.tools.MainClassFinder; + +import java.io.File; +import java.io.IOException; + +/** + * Help find main class and set it to the specified property. + * + * @author Phillip Webb + * @author Dave Syer + * @author Stephane Nicoll + * @author Björn Lindström + * @author Scott Frederick + * @author kerwin612 + * @since 1.0.0 + */ +@Mojo(name = "fmc", defaultPhase = LifecyclePhase.PREPARE_PACKAGE, requiresProject = true, threadSafe = true, + requiresDependencyResolution = ResolutionScope.RUNTIME) +@Execute(phase = LifecyclePhase.PREPARE_PACKAGE) +public class FindMainClassMojo extends AbstractDependencyFilterMojo { + + private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication"; + + /** + * The name of the main class. If not specified the first compiled class found that + * contains a 'main' method will be used. + * @since 1.0.0 + */ + @Parameter(property = "spring-boot.run.main-class") + private String mainClass; + + /** + * Directory containing the classes and resource files that should be packaged into + * the archive. + * @since 1.0.0 + */ + @Parameter(defaultValue = "${project.build.outputDirectory}", required = true) + private File classesDirectory; + + @Parameter(defaultValue = "mainClass", required = true) + private String mainClassPropertyName; + + @Parameter(defaultValue = "${project}", readonly = true, required = true) + private MavenProject project; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + String mainClass = getStartClass(); + if (mainClass == null) { + throw new MojoExecutionException("Unable to find a suitable main class"); + } + getLog().info("found main class: " + mainClass); + project.getProperties().put(mainClassPropertyName, mainClass); + } + + protected String getStartClass() throws MojoExecutionException { + String mainClass = this.mainClass; + if (mainClass == null) { + try { + mainClass = MainClassFinder.findSingleMainClass(this.classesDirectory, + SPRING_BOOT_APPLICATION_CLASS_NAME); + } + catch (IOException ex) { + throw new MojoExecutionException(ex.getMessage(), ex); + } + } + if (mainClass == null) { + throw new MojoExecutionException("Unable to find a suitable main class, please add a 'mainClass' property"); + } + return mainClass; + } + +}