Angular 2和Spring Boot - 部署到战争

Angular 2和Spring Boot - 部署到战争

问题描述:

让我刚开始说,我是Maven/Spring的新手,并且很难弄清楚当我的目录不遵循首选的Maven结构时该怎么做。Angular 2和Spring Boot - 部署到战争

我按照说明通过这个tutorial设置了Angular 2和Spring Boot的项目。本教程创建了两个模块,前端和后端,包含相应的pom.xml和一个父pom.xml。从后端目录:“运行mvn春天启动”我可以,或者通过运行运行应用程序就好用我的IDE,的IntelliJ。但是,为了部署,我希望将应用程序打包成WAR文件以放入Tomcat服务器。我不确定如何使用我目前拥有的pom.xml来做到这一点。我敢肯定,它与我的目录结构做的,但我不知道我是否应该调整自己的应用程序或者有配置Maven的两个模块放到导致WAR文件按预期工作的方式。

我已经找到类似的答案here但最后一部分是什么让我失望。我没有/ src/main/webapp/WEB-INF文件夹,我不确定在哪里做。

我的应用程序结构如下:

AppRoot 

-backend 
--src 
---main 
----java 
--pom.xml 

-frontend 
--src 
---main 
----frontend 
--pom.xml 

-pom.xml 

我的根pom.xml的是:

<groupId>com.trinityinnovations</groupId> 
<artifactId>parent</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>pom</packaging> 

<name>c-cop</name> 
<description>C-COP Project</description> 

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

<modules> 
    <module>frontend</module> 
    <module>backend</module> 
<module>web</module> 

前端的pom.xml:

<artifactId>frontend</artifactId> 

<name>frontend</name> 
<description>C-COP Project frontend</description> 

<parent> 
    <groupId>com.trinityinnovations</groupId> 
    <artifactId>parent</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
</parent> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>com.github.eirslett</groupId> 
      <artifactId>frontend-maven-plugin</artifactId> 
      <version>1.3</version> 

      <configuration> 
       <nodeVersion>v6.9.1</nodeVersion> 
       <npmVersion>4.0.3</npmVersion> 
       <workingDirectory>src/main/frontend</workingDirectory> 
      </configuration> 

      <executions> 
       <execution> 
        <id>install node and npm</id> 
        <goals> 
         <goal>install-node-and-npm</goal> 
        </goals> 
       </execution> 

       <execution> 
        <id>npm install</id> 
        <goals> 
         <goal>npm</goal> 
        </goals> 
       </execution> 

       <execution> 
        <id>npm run build</id> 
        <goals> 
         <goal>npm</goal> 
        </goals> 

        <configuration> 
         <arguments>run build</arguments> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    <resources> 
     <resource> 
      <directory>target/frontend</directory> 
      <targetPath>static</targetPath> 
     </resource> 
    </resources> 
</build> 

后端POM .xml:

<artifactId>backend</artifactId> 

<name>backend</name> 
<description>C-COP Project backend</description> 

<parent> 
    <groupId>com.trinityinnovations</groupId> 
    <artifactId>parent</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
</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-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.restdocs</groupId> 
     <artifactId>spring-restdocs-mockmvc</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.trinityinnovations</groupId> 
     <artifactId>frontend</artifactId> 
     <version>${project.version}</version> 
     <scope>runtime</scope> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> 
    <dependency> 
     <groupId>commons-httpclient</groupId> 
     <artifactId>commons-httpclient</artifactId> 
     <version>3.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-csv</artifactId> 
     <version>1.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>6.0.6</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.datatype</groupId> 
     <artifactId>jackson-datatype-hibernate5</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.javaetmoi.core</groupId> 
     <artifactId>javaetmoi-hibernate5-hydrate</artifactId> 
     <version>2.3</version> 
    </dependency> 
<dependency> 
    <groupId>com.google.maps</groupId> 
    <artifactId>google-maps-services</artifactId> 
    <version>0.1.20</version> 
</dependency> 
</dependencies> 

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

请让我知道是否有任何需要的信息。

经过大量搜索,我遇到Maven War Plugin。这使我可以将必要的前端文件提交到后端,以便成功创建我的WAR文件。是需要进行变动如下:

后端的pom.xml

- 后的描述标签添加:

<packaging>war</packaging> 

然后,构建标签里面,里面添加插件这个插件:

<plugin> 
    <artifactId>maven-war-plugin</artifactId> 
    <configuration> 
     <webResources> 
     <resource> 
      <directory>../frontend/target/frontend</directory> 
     </resource> 
     </webResources> 
    </configuration> 
    </plugin> 

除此之外,您可以保留现有的pom.xml,因为只有后端pom.xml需要包含war包装。它最终成为一个相当简单的答案。

还需要在package.json中设置base-href。注意“建”:在我的项目我没有后台

"scripts": { 
"ng": "ng", 
"start": "ng serve --proxy-config proxy.conf.json", 
"test": "ng test", 
"lint": "ng lint", 
"e2e": "ng e2e", 
"build": "ng build --base-href=\"./\"" 
}, 
+0

,你有什么想法,我应该在哪里抄我DIST夹在春季启动项目,将其部署为WAR文件? – Shilan

+0

我不完全确定,但我很好奇......如果你没有后端,为什么你的项目需要被包装成战争文件? – landesko

+0

:)有一个后端,只包含宁静的服务,但我不能将角码与该代码放在一个模块中,基本上我不允许修改其他后端模块,我只能调用restful服务。 – Shilan

参考文档中包含的这个详细描述。你需要在你的前端和后台模块pom和一些Java代码中使用<packaging>war</packaging>。所有描述here

这样说,我尽量避免战争部署,如果不是完全必要的话。你可以只运行带有java -jar your.jar内置的jar文件,它会在一个嵌入式的Tomcat启动。