2017-03-16 95 views
0

我已經使用Spring初始化程序,嵌入式Tomcat,Thymeleaf模板引擎生成了Spring Boot Web應用程序。使用的技術:Spring Boot 1.4.2.RELEASE,Spring 4.3.4。 RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat的嵌入8.5.6時,Maven 3,Java的8 我有這個pom.xml文件,SpringBoot:定義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); 
    } 

} 
+0

編輯問題以包含您的主要課程。 –

回答

2

Maven輪廓沒有任何關係Spring配置文件。

你有兩個Maven概況:罐子,戰爭

你要麼使用:

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

建設這件神器的罐子時

爲部署到一個servlet容器war文件時所建使用-Pwar Maven配置文件。

1

Maven的配置文件和配置文件春季是兩個不同的概念。

您還應該使用war配置文件運行Maven,以便使用正確的匹配值設置spring.boot.mainclass屬性。

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

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