2014-03-01 44 views
9

我想將彈簧引導應用程序打包爲jar,我使用mvn package來打包。打包包含JSP和靜態資源的彈簧引導應用程序

這產生了一個不包含任何/WEB-INF/jsp/src/main/webapp/resources的罐子。

如何確保我的jar包含所需的一切?

這裏我目前pom.xml

<modelVersion>4.0.0</modelVersion> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-samples</artifactId> 
    <version>1.0.0.RC3</version> 
</parent> 

<packaging>jar</packaging> 

<properties> 
    <main.basedir>${basedir}/../..</main.basedir> 
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot> 
</properties> 

<dependencies> 

    <dependency> 
     <groupId>${project.groupId}</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>${project.groupId}</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>tomcat-jdbc</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.3-1101-jdbc41</version> 
    </dependency> 

</dependencies> 

<!-- Package as an executable JAR --> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
       <useSystemClassLoader>false</useSystemClassLoader> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<!-- Allow access to Spring milestones and snapshots --> 
<!-- (you don't need this if you are using anything after 1.0.0.RELEASE) --> 
<repositories> 
    <repository> 
     <id>spring-snapshots</id> 
     <url>http://repo.spring.io/snapshot</url> 
     <snapshots> 
      <enabled>true</enabled> 
     </snapshots> 
    </repository> 
    <repository> 
     <id>spring-milestones</id> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>true</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
<pluginRepositories> 
    <pluginRepository> 
     <id>spring-snapshots</id> 
     <url>http://repo.spring.io/snapshot</url> 
    </pluginRepository> 
    <pluginRepository> 
     <id>spring-milestones</id> 
     <url>http://repo.spring.io/milestone</url> 
    </pluginRepository> 
</pluginRepositories> 
<artifactId>com.example.app</artifactId> 

回答

9

以下示例適用於春引導1.3.3.RELEASE: https://github.com/ghillert/spring-boot-jsp-demo

的關鍵是把靜態JSP內容:

/src/main/resources/META-INF/resources/WEB-INF/jsp 

,並確保你定義視圖前綴在application.properties /後綴:

spring.mvc.view.prefix=/WEB-INF/jsp/ 
spring.mvc.view.suffix=.jsp 
+0

這不適用於我:( – Arun

5

有什麼理由,爲什麼你不能用戰爭的包裝類型? https://maven.apache.org/plugins/maven-war-plugin/usage.html 我會推薦使用war包裝類型並使用默認的maven web應用程序結構。

如果你真的想爲你的webapp使用jar插件,你需要爲你的項目配置它。由於你的發佈,我不瞭解你的結構,也不能給你一個例子。退房Jar插件這裏的用法:https://maven.apache.org/plugins/maven-war-plugin/usage.html

+2

您需要使用戰爭包裝,但否則它看起來都很健全。請參閱JSP限制中的[docs](https://github.com/spring-projects/spring-boot/blob/master/spring-boot/README.md#jsp-limitations)。 –

+3

我已經嘗試過與戰爭包裝,它的工作原理。 java -jar myfile.war看起來有點奇怪,但它有效。謝謝 ! – yglodt

1

要創建具有春天啓動運行的JAR,使用彈簧啓動了Maven插件在你的pom.xml

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

也許你想有一個看看我的示例應用程序http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8/(源代碼位於GitHub上,應用程序正在運行並從JAR運行)。

您應該注意的一件事:由於某些嵌入式Tomcat問題,來自JAR的JSP不起作用。我正在使用Thymeleaf。如果您需要JSP,請繼續使用WAR部署。

3

您的構建標籤更改爲

<build> 
<resources> 
      <resource> 
       <directory>${basedir}/src/main/webapp</directory> 
       <includes> 
        <include>**/**</include> 
       </includes> 
      </resource> 
      <resource> 
       <directory>${basedir}/src/main/resources</directory> 
       <includes> 
        <include>**/**</include> 
       </includes> 
      </resource> 
     </resources> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <useSystemClassLoader>false</useSystemClassLoader> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
相關問題