SpringBoot:在pom.xml文件中定义主类

时间:2017-03-16 18:53:02

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

我使用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);
    }

}

2 个答案:

答案 0 :(得分:2)

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

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

-Pwar添加到命令行以激活war Maven个人资料。

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

答案 1 :(得分:2)

double d = 10.0; std::shared_ptr<int> int_ptr = my_make_shared(d); // ERROR 个人资料与Maven个人资料无关。

您有两个Spring个人资料:jar,war

您可以使用:

Maven

mvn clean package -Pjar

您在构建工件时遇到的错误是因为mvn clean package -Pwar 需要:

pom

在properties元素中。

无论<start-class>your.app.entry.point.Main</start-class> 个人资料如何,我都建议只有一个开始课程。

类似的东西:

<强>的pom.xml

Maven

<强> Main.java

...
<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>
...

无需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

将此工件构建为jar

作为使用-Pwar java -jar ..... 配置文件构建时部署到servlet容器的war文件。