Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot with no main class

I am trying use Spring Boot, and create a jar, and install it into my maven repo.

This is a library jar file that will be used as a dependency in my main application, which is also a Spring-Boot application. Right now, I am just working on a hello world example. here is my one class in this project:

public class MyLibrary {

  public String getMessage() {
    return "Hello World!";
  }
}

and my POM basically looks like this:

<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<groupId>com.me.plugin</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

<dependencies> 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

But when I try to build, I get 'Unable to find main class" error. Which makes perfect sense to me, but how do I get spring to make a library jar. I know I could add an Application.java with a main method, but that's not what I want, I want a simple jar file, but with all the Spring-Boot annotations, injections, etc...

How would I go about doing this? Is spring boot not what I want here?

like image 206
mmaceachran Avatar asked Oct 22 '17 18:10

mmaceachran


People also ask

Can I run spring boot without main class?

When Spring boot application starts: If we define no main class, Spring Boot will search for the main class in the class path. It will fail, we there is no main class or we have multiple classes with the main method.

Can we have two main classes in spring boot?

Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM.

Can we run spring boot application without @SpringBootApplication?

It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in the next point.

What is default classpath in spring boot?

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.


1 Answers

I am sure you are using spring-boot-maven-plugin in your pom.xml (as given below), which allows you to package executable jar or war archives and run an application.

With this plugin, spring searches for a main application.You don't need this plugin for a spring library project. Delete this plugin and clean install .

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

I assume you are creating some spring configuration files or components in this library project. You may not need big fat spring-boot-starter. But its fine if you need multiple spring modules and you want one place for all dependency.

like image 72
surya Avatar answered Sep 29 '22 21:09

surya