4

I am trying to build a project with

apply plugin: 'spring-boot'

But it throws an exception at the step startScripts

:common:compileJava UP-TO-DATE
:common:processResources UP-TO-DATE
:common:classes UP-TO-DATE
:common:jar UP-TO-DATE
:common:findMainClass
:common:startScripts FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':common:startScripts'.
> No value has been specified for property 'mainClassName'.

But I really do not need a main Class for this module. How should I settle this?

David
  • 420
  • 2
  • 5
  • 18

3 Answers3

2

There's an open ticket about this issue on Spring-Boot's GitHub: https://github.com/spring-projects/spring-boot/issues/2679. There are couple things you can do according to discussion:

  • Remove spring-boot plugin from buildscript section. This will remove special Gradle tasks from build flow.
  • Assign mainClassName to some random string. It's not pretty but as far as this is not runnable app you probably don't care about main class.
  • There are some more bulky ways to solve this listed by the link if you want to try them.

Hope this helps.

eduard.dudar
  • 111
  • 2
  • 5
1

You need a main method which calls SpringApplication.run(...). Like this:

public static void main(String[] args) throws Exception {
  SpringApplication.run(Example.class, args);
}

In the Gettings started guide is a paragraph about the main method:

The final part of our application is the main method. This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication will bootstrap our application, starting Spring which will in turn start the auto-configured Tomcat web server. We need to pass Example.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.

I recommend to create a Application class where it all starts:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

But, perhaps you should start with the Quick start guide at first.

alexvetter
  • 1,998
  • 2
  • 16
  • 42
  • How about a library module, which is not necessary to have a main class, right? – David May 20 '15 at 08:12
  • If it's a library than you don't need the `spring-boot` plugin. What should the library do? – alexvetter May 20 '15 at 08:15
  • A `spring-boot` application is a self-contained application. If you have a library than it is not a `spring-boot` application. – alexvetter May 20 '15 at 08:18
  • Without spring-boot plugin, the way of config gradle will be different, for example, I cannot use testCompile("junit:junit") to add a dependency. I also cannot use those features provided by the plugin. – David May 20 '15 at 08:19
  • 2
    Of course you can have this dependency. You just need a simple `build.gradle` with `apply plugin: 'java' repositories { mavenCentral() } dependencies { testCompile 'junit:junit:4.12' }`. – alexvetter May 20 '15 at 08:22
  • OK. If that is the case, I guess I should only apply the spring-boot plugin for the spring-boot modules, and have different config for non spring-boot modules. A little bit troublesome, but if have no choice, then I will do it. – David May 20 '15 at 08:25
  • I see. The thing is testCompile("junit:junit") may be using junit 4.11 rather than 4.12. So I need to tidy up the versions manually. – David May 20 '15 at 08:28
  • Ok, you have a `spring-boot` project but also some other projects like `common`? Than you need to setup a multi-project gradle configuration. I found a [blog post](https://mhdevelopment.wordpress.com/2014/09/16/how-to-configure-spring-boot-with-jpa-and-multiple-projects/) about that. – alexvetter May 20 '15 at 08:28
0

I have the following at the end of my build.gradle to define the name of the main class.

I also use gradlew bootjar instead of gradlew build.

bootJar {
    mainClassName = 'com.spring.xx.app.App'
}
Pingolin
  • 3,161
  • 6
  • 25
  • 40