Commit 1595286e authored by Andy Wilkinson's avatar Andy Wilkinson

Prefer @SpringBootApplication-annotated class when finding main class

Closes gh-6496
parent ba61faee
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,6 +35,8 @@ import org.springframework.util.StringUtils; ...@@ -35,6 +35,8 @@ import org.springframework.util.StringUtils;
*/ */
public class FindMainClass extends Task { public class FindMainClass extends Task {
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
private String mainClass; private String mainClass;
private File classesRoot; private File classesRoot;
...@@ -70,10 +72,11 @@ public class FindMainClass extends Task { ...@@ -70,10 +72,11 @@ public class FindMainClass extends Task {
} }
try { try {
if (this.classesRoot.isDirectory()) { if (this.classesRoot.isDirectory()) {
return MainClassFinder.findSingleMainClass(this.classesRoot); return MainClassFinder.findSingleMainClass(this.classesRoot,
SPRING_BOOT_APPLICATION_CLASS_NAME);
} }
return MainClassFinder.findSingleMainClass(new JarFile(this.classesRoot), return MainClassFinder.findSingleMainClass(new JarFile(this.classesRoot), "/",
"/"); SPRING_BOOT_APPLICATION_CLASS_NAME);
} }
catch (IOException ex) { catch (IOException ex) {
throw new BuildException(ex); throw new BuildException(ex);
......
...@@ -41,6 +41,8 @@ import org.springframework.boot.loader.tools.MainClassFinder; ...@@ -41,6 +41,8 @@ import org.springframework.boot.loader.tools.MainClassFinder;
*/ */
public class FindMainClassTask extends DefaultTask { public class FindMainClassTask extends DefaultTask {
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
@Input @Input
private SourceSetOutput mainClassSourceSetOutput; private SourceSetOutput mainClassSourceSetOutput;
...@@ -105,7 +107,8 @@ public class FindMainClassTask extends DefaultTask { ...@@ -105,7 +107,8 @@ public class FindMainClassTask extends DefaultTask {
+ this.mainClassSourceSetOutput.getClassesDir()); + this.mainClassSourceSetOutput.getClassesDir());
try { try {
mainClass = MainClassFinder.findSingleMainClass( mainClass = MainClassFinder.findSingleMainClass(
this.mainClassSourceSetOutput.getClassesDir()); this.mainClassSourceSetOutput.getClassesDir(),
SPRING_BOOT_APPLICATION_CLASS_NAME);
project.getLogger().info("Computed main class: " + mainClass); project.getLogger().info("Computed main class: " + mainClass);
} }
catch (IOException ex) { catch (IOException ex) {
......
...@@ -59,6 +59,8 @@ public class Repackager { ...@@ -59,6 +59,8 @@ public class Repackager {
private static final long FIND_WARNING_TIMEOUT = TimeUnit.SECONDS.toMillis(10); private static final long FIND_WARNING_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
private List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<MainClassTimeoutWarningListener>(); private List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<MainClassTimeoutWarningListener>();
private String mainClass; private String mainClass;
...@@ -383,7 +385,7 @@ public class Repackager { ...@@ -383,7 +385,7 @@ public class Repackager {
protected String findMainMethod(JarFile source) throws IOException { protected String findMainMethod(JarFile source) throws IOException {
return MainClassFinder.findSingleMainClass(source, return MainClassFinder.findSingleMainClass(source,
this.layout.getClassesLocation()); this.layout.getClassesLocation(), SPRING_BOOT_APPLICATION_CLASS_NAME);
} }
private void renameFile(File file, File dest) { private void renameFile(File file, File dest) {
......
...@@ -26,7 +26,9 @@ import org.junit.Test; ...@@ -26,7 +26,9 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.tools.MainClassFinder.ClassNameCallback; import org.springframework.boot.loader.tools.MainClassFinder.MainClass;
import org.springframework.boot.loader.tools.MainClassFinder.MainClassCallback;
import org.springframework.boot.loader.tools.sample.AnnotatedClassWithMainMethod;
import org.springframework.boot.loader.tools.sample.ClassWithMainMethod; import org.springframework.boot.loader.tools.sample.ClassWithMainMethod;
import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod; import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod;
...@@ -87,6 +89,16 @@ public class MainClassFinderTests { ...@@ -87,6 +89,16 @@ public class MainClassFinderTests {
MainClassFinder.findSingleMainClass(this.testJarFile.getJarFile(), ""); MainClassFinder.findSingleMainClass(this.testJarFile.getJarFile(), "");
} }
@Test
public void findSingleJarSearchPrefersAnnotatedMainClass() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", AnnotatedClassWithMainMethod.class);
String mainClass = MainClassFinder.findSingleMainClass(
this.testJarFile.getJarFile(), "",
"org.springframework.boot.loader.tools.sample.SomeApplication");
assertThat(mainClass).isEqualTo("a.b.c.E");
}
@Test @Test
public void findMainClassInJarSubLocation() throws Exception { public void findMainClassInJarSubLocation() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
...@@ -132,6 +144,16 @@ public class MainClassFinderTests { ...@@ -132,6 +144,16 @@ public class MainClassFinderTests {
MainClassFinder.findSingleMainClass(this.testJarFile.getJarSource()); MainClassFinder.findSingleMainClass(this.testJarFile.getJarSource());
} }
@Test
public void findSingleFolderSearchPrefersAnnotatedMainClass() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", AnnotatedClassWithMainMethod.class);
String mainClass = MainClassFinder.findSingleMainClass(
this.testJarFile.getJarSource(),
"org.springframework.boot.loader.tools.sample.SomeApplication");
assertThat(mainClass).isEqualTo("a.b.c.E");
}
@Test @Test
public void doWithFolderMainMethods() throws Exception { public void doWithFolderMainMethods() throws Exception {
this.testJarFile.addClass("a/b/c/D.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/b/c/D.class", ClassWithMainMethod.class);
...@@ -150,17 +172,17 @@ public class MainClassFinderTests { ...@@ -150,17 +172,17 @@ public class MainClassFinderTests {
this.testJarFile.addClass("a/b/F.class", ClassWithoutMainMethod.class); this.testJarFile.addClass("a/b/F.class", ClassWithoutMainMethod.class);
this.testJarFile.addClass("a/b/G.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/b/G.class", ClassWithMainMethod.class);
ClassNameCollector callback = new ClassNameCollector(); ClassNameCollector callback = new ClassNameCollector();
MainClassFinder.doWithMainClasses(this.testJarFile.getJarFile(), "", callback); MainClassFinder.doWithMainClasses(this.testJarFile.getJarFile(), null, callback);
assertThat(callback.getClassNames().toString()).isEqualTo("[a.b.G, a.b.c.D]"); assertThat(callback.getClassNames().toString()).isEqualTo("[a.b.G, a.b.c.D]");
} }
private static class ClassNameCollector implements ClassNameCallback<Object> { private static class ClassNameCollector implements MainClassCallback<Object> {
private final List<String> classNames = new ArrayList<String>(); private final List<String> classNames = new ArrayList<String>();
@Override @Override
public Object doWith(String className) { public Object doWith(MainClass mainClass) {
this.classNames.add(className); this.classNames.add(mainClass.getName());
return null; return null;
} }
......
/*
* Copyright 2012-2016 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
*
* http://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.loader.tools.sample;
/**
* Sample annotated class with a main method.
*
* @author Andy Wilkinson
*/
@SomeApplication
public class AnnotatedClassWithMainMethod {
public void run() {
System.out.println("Hello World");
}
public static void main(String[] args) {
new AnnotatedClassWithMainMethod().run();
}
}
/*
* Copyright 2012-2016 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
*
* http://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.loader.tools.sample;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Test annotation for a main application class.
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface SomeApplication {
}
...@@ -52,6 +52,8 @@ import org.springframework.boot.loader.tools.MainClassFinder; ...@@ -52,6 +52,8 @@ import org.springframework.boot.loader.tools.MainClassFinder;
*/ */
public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
private static final String SPRING_LOADED_AGENT_CLASS_NAME = "org.springsource.loaded.agent.SpringLoadedAgent"; private static final String SPRING_LOADED_AGENT_CLASS_NAME = "org.springsource.loaded.agent.SpringLoadedAgent";
/** /**
...@@ -374,7 +376,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { ...@@ -374,7 +376,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
String mainClass = this.mainClass; String mainClass = this.mainClass;
if (mainClass == null) { if (mainClass == null) {
try { try {
mainClass = MainClassFinder.findSingleMainClass(this.classesDirectory); mainClass = MainClassFinder.findSingleMainClass(this.classesDirectory,
SPRING_BOOT_APPLICATION_CLASS_NAME);
} }
catch (IOException ex) { catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex); throw new MojoExecutionException(ex.getMessage(), ex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment