【问题标题】:SpringBoot: Defining the main class in pom.xml fileSpringBoot:在 pom.xml 文件中定义主类
【发布时间】:2017-08-08 03:05:25
【问题描述】:

我使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序。使用的技术:Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat Embed 8.5.6、Maven 3、Java 8。 我有这个 pom.xml 文件,

我使用这个命令生成战争

mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bookcloud</groupId>
    <artifactId>bookcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>bookcloud</name>
    <description>Book Cloud </description>

    <profiles>
        <profile>
            <id>jar</id>
            <properties>
                <spring.boot.mainclass>com.bookcloud.iot.BookCloudApplication</spring.boot.mainclass>
            </properties>
        </profile>
        <profile>
            <id>war</id>
            <properties>
            <spring.boot.mainclass>com.bookcloud.iot.BookCloudApplicationWar</spring.boot.mainclass>
            </properties>
        </profile>
    </profiles>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency> 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
         <dependency>
            <groupId>it.ozimov</groupId>
            <artifactId>spring-boot-email-core</artifactId>
            <version>0.5.1</version>
        </dependency>
        <dependency>
            <groupId>it.ozimov</groupId>
            <artifactId>spring-boot-thymeleaf-email</artifactId>
            <version>0.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/com.icegreen/greenmail -->
        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail</artifactId>
            <version>1.5.3</version>
            <optional>true</optional>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>     

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>


        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>               
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>${spring.boot.mainclass}</mainClass>
                         </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

但我仍然收到此错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project bookcloud: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.bookcloud.iot.BookCloudApplication, com.bookcloud.iot.BookCloudApplicationWar] -> [Help 1]

BookCloudApplication.java

@Profile("!war")
@SpringBootApplication
@Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class BookCloudApplication {

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

BookCloudApplicationWar.java

@Profile("war")
@Import({SecurityConfig.class ,PersistenceConfig.class})
@SpringBootApplication
public class BookCloudApplicationWar extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BookCloudApplicationWar.class);
    }

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

}

【问题讨论】:

  • 编辑问题以包含您的主要课程。

标签: spring maven spring-mvc spring-boot spring-profiles


【解决方案1】:

Maven 个人资料与Spring 个人资料无关。

您有两个Maven 配置文件:jar、war

您可以使用:

mvn clean package -Pjar

mvn clean package -Pwar

您遇到的错误是在构建工件时,因为pom 需要:

<start-class>your.app.entry.point.Main</start-class>

在属性元素内。

我建议无论Maven 个人资料如何,都只开设一个开始课程。

类似:

pom.xml

...
<modelVersion>4.0.0</modelVersion>
<groupId>com.asimio.cloud</groupId>
<artifactId>zuul-server</artifactId>
<version>0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
...
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <start-class>your.app.entry.point.Main</start-class>
  <packaging.type>jar</packaging.type>
...
<profiles>
  <profile>
    <id>war</id>
    <properties>
      <packaging.type>war</packaging.type>
    </properties>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
...
  </profile>
</profiles>
...

Main.java

package your.app.entry.point;
...
@SpringBootApplication
@Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class App extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(App.class);
  }

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

不需要Spring 个人资料。现在可以使用 Maven 配置文件构建此工件,并将使用以下任一配置运行:

java -jar .....

将此工件构建为 jar 时

当使用 -Pwar Maven 配置文件构建时,作为部署到 servlet 容器的 war 文件。

【讨论】:

    【解决方案2】:

    Maven 配置文件和 Spring 配置文件是两个不同的概念。

    您还应该使用 war 配置文件运行 Maven,以便使用正确的匹配值设置 spring.boot.mainclass 属性。

    -Pwar 添加到命令行以激活war Maven 配置文件。

    mvn clean package -DskipTests -Pwar -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 2022-01-05
      • 1970-01-01
      • 2017-04-03
      • 2022-06-16
      • 2012-08-19
      • 2020-11-25
      • 1970-01-01
      • 2018-01-23
      相关资源
      最近更新 更多