Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
1595286e
Commit
1595286e
authored
Nov 29, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prefer @SpringBootApplication-annotated class when finding main class
Closes gh-6496
parent
ba61faee
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
287 additions
and
55 deletions
+287
-55
FindMainClass.java
...main/java/org/springframework/boot/ant/FindMainClass.java
+7
-4
FindMainClassTask.java
...rg/springframework/boot/gradle/run/FindMainClassTask.java
+4
-1
MainClassFinder.java
...rg/springframework/boot/loader/tools/MainClassFinder.java
+174
-43
Repackager.java
...ava/org/springframework/boot/loader/tools/Repackager.java
+3
-1
MainClassFinderTests.java
...ringframework/boot/loader/tools/MainClassFinderTests.java
+27
-5
AnnotatedClassWithMainMethod.java
...oot/loader/tools/sample/AnnotatedClassWithMainMethod.java
+35
-0
SomeApplication.java
...ngframework/boot/loader/tools/sample/SomeApplication.java
+33
-0
AbstractRunMojo.java
.../java/org/springframework/boot/maven/AbstractRunMojo.java
+4
-1
No files found.
spring-boot-tools/spring-boot-antlib/src/main/java/org/springframework/boot/ant/FindMainClass.java
View file @
1595286e
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
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.
...
...
@@ -35,6 +35,8 @@ import org.springframework.util.StringUtils;
*/
public
class
FindMainClass
extends
Task
{
private
static
final
String
SPRING_BOOT_APPLICATION_CLASS_NAME
=
"org.springframework.boot.autoconfigure.SpringBootApplication"
;
private
String
mainClass
;
private
File
classesRoot
;
...
...
@@ -70,10 +72,11 @@ public class FindMainClass extends Task {
}
try
{
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
)
{
throw
new
BuildException
(
ex
);
...
...
spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/run/FindMainClassTask.java
View file @
1595286e
...
...
@@ -41,6 +41,8 @@ import org.springframework.boot.loader.tools.MainClassFinder;
*/
public
class
FindMainClassTask
extends
DefaultTask
{
private
static
final
String
SPRING_BOOT_APPLICATION_CLASS_NAME
=
"org.springframework.boot.autoconfigure.SpringBootApplication"
;
@Input
private
SourceSetOutput
mainClassSourceSetOutput
;
...
...
@@ -105,7 +107,8 @@ public class FindMainClassTask extends DefaultTask {
+
this
.
mainClassSourceSetOutput
.
getClassesDir
());
try
{
mainClass
=
MainClassFinder
.
findSingleMainClass
(
this
.
mainClassSourceSetOutput
.
getClassesDir
());
this
.
mainClassSourceSetOutput
.
getClassesDir
(),
SPRING_BOOT_APPLICATION_CLASS_NAME
);
project
.
getLogger
().
info
(
"Computed main class: "
+
mainClass
);
}
catch
(
IOException
ex
)
{
...
...
spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java
View file @
1595286e
This diff is collapsed.
Click to expand it.
spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java
View file @
1595286e
...
...
@@ -59,6 +59,8 @@ public class Repackager {
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
String
mainClass
;
...
...
@@ -383,7 +385,7 @@ public class Repackager {
protected
String
findMainMethod
(
JarFile
source
)
throws
IOException
{
return
MainClassFinder
.
findSingleMainClass
(
source
,
this
.
layout
.
getClassesLocation
());
this
.
layout
.
getClassesLocation
()
,
SPRING_BOOT_APPLICATION_CLASS_NAME
);
}
private
void
renameFile
(
File
file
,
File
dest
)
{
...
...
spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/MainClassFinderTests.java
View file @
1595286e
...
...
@@ -26,7 +26,9 @@ import org.junit.Test;
import
org.junit.rules.ExpectedException
;
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.ClassWithoutMainMethod
;
...
...
@@ -87,6 +89,16 @@ public class MainClassFinderTests {
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
public
void
findMainClassInJarSubLocation
()
throws
Exception
{
this
.
testJarFile
.
addClass
(
"a/B.class"
,
ClassWithMainMethod
.
class
);
...
...
@@ -132,6 +144,16 @@ public class MainClassFinderTests {
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
public
void
doWithFolderMainMethods
()
throws
Exception
{
this
.
testJarFile
.
addClass
(
"a/b/c/D.class"
,
ClassWithMainMethod
.
class
);
...
...
@@ -150,17 +172,17 @@ public class MainClassFinderTests {
this
.
testJarFile
.
addClass
(
"a/b/F.class"
,
ClassWithoutMainMethod
.
class
);
this
.
testJarFile
.
addClass
(
"a/b/G.class"
,
ClassWithMainMethod
.
class
);
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]"
);
}
private
static
class
ClassNameCollector
implements
ClassName
Callback
<
Object
>
{
private
static
class
ClassNameCollector
implements
MainClass
Callback
<
Object
>
{
private
final
List
<
String
>
classNames
=
new
ArrayList
<
String
>();
@Override
public
Object
doWith
(
String
className
)
{
this
.
classNames
.
add
(
className
);
public
Object
doWith
(
MainClass
mainClass
)
{
this
.
classNames
.
add
(
mainClass
.
getName
()
);
return
null
;
}
...
...
spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/sample/AnnotatedClassWithMainMethod.java
0 → 100644
View file @
1595286e
/*
* 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
();
}
}
spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/sample/SomeApplication.java
0 → 100644
View file @
1595286e
/*
* 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
{
}
spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java
View file @
1595286e
...
...
@@ -52,6 +52,8 @@ import org.springframework.boot.loader.tools.MainClassFinder;
*/
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"
;
/**
...
...
@@ -374,7 +376,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
String
mainClass
=
this
.
mainClass
;
if
(
mainClass
==
null
)
{
try
{
mainClass
=
MainClassFinder
.
findSingleMainClass
(
this
.
classesDirectory
);
mainClass
=
MainClassFinder
.
findSingleMainClass
(
this
.
classesDirectory
,
SPRING_BOOT_APPLICATION_CLASS_NAME
);
}
catch
(
IOException
ex
)
{
throw
new
MojoExecutionException
(
ex
.
getMessage
(),
ex
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment