Angular 2和Spring Boot – 部署到战争

前端之家收集整理的这篇文章主要介绍了Angular 2和Spring Boot – 部署到战争前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
让我先说一下,我是Maven / Spring的新手,当我的目录不遵循首选的Maven结构时,我很难搞清楚要做什么.

我通过这个tutorial遵循了使用Angular 2和Spring Boot设置项目的说明.本教程创建了两个模块,前端和后端,以及相应的pom.xml和一个父pom.xml.我可以使用我的IDE,IntelliJ或从后端目录运行“mvn spring-boot:run”来运行应用程序.但是,对于部署,我希望将应用程序打包到WAR文件中以放入Tomcat服务器.我不确定如何使用我目前拥有的pom.xml来做到这一点.我很确定它与我的目录结构有关,但是我不确定是否应该重构我的应用程序,或者有一种方法可以配置Maven将这两个模块放入生成预期的WAR文件中.

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

我的申请结构如下:

  1. AppRoot
  2.  
  3. -backend
  4. --src
  5. ---main
  6. ----java
  7. --pom.xml
  8.  
  9. -frontend
  10. --src
  11. ---main
  12. ----frontend
  13. --pom.xml
  14.  
  15. -pom.xml

我的root pom.xml是:

  1. <groupId>com.trinityinnovations</groupId>
  2. <artifactId>parent</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>pom</packaging>
  5.  
  6. <name>c-cop</name>
  7. <description>C-COP Project</description>
  8.  
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.2.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15.  
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21.  
  22. <modules>
  23. <module>frontend</module>
  24. <module>backend</module>
  25. <module>web</module>

前端pom.xml:

  1. <artifactId>frontend</artifactId>
  2.  
  3. <name>frontend</name>
  4. <description>C-COP Project frontend</description>
  5.  
  6. <parent>
  7. <groupId>com.trinityinnovations</groupId>
  8. <artifactId>parent</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. </parent>
  11.  
  12. <build>
  13. <plugins>
  14. <plugin>
  15. <groupId>com.github.eirslett</groupId>
  16. <artifactId>frontend-maven-plugin</artifactId>
  17. <version>1.3</version>
  18.  
  19. <configuration>
  20. <nodeVersion>v6.9.1</nodeVersion>
  21. <npmVersion>4.0.3</npmVersion>
  22. <workingDirectory>src/main/frontend</workingDirectory>
  23. </configuration>
  24.  
  25. <executions>
  26. <execution>
  27. <id>install node and npm</id>
  28. <goals>
  29. <goal>install-node-and-npm</goal>
  30. </goals>
  31. </execution>
  32.  
  33. <execution>
  34. <id>npm install</id>
  35. <goals>
  36. <goal>npm</goal>
  37. </goals>
  38. </execution>
  39.  
  40. <execution>
  41. <id>npm run build</id>
  42. <goals>
  43. <goal>npm</goal>
  44. </goals>
  45.  
  46. <configuration>
  47. <arguments>run build</arguments>
  48. </configuration>
  49. </execution>
  50. </executions>
  51. </plugin>
  52. </plugins>
  53. <resources>
  54. <resource>
  55. <directory>target/frontend</directory>
  56. <targetPath>static</targetPath>
  57. </resource>
  58. </resources>
  59. </build>

后端pom.xml:

  1. <artifactId>backend</artifactId>
  2.  
  3. <name>backend</name>
  4. <description>C-COP Project backend</description>
  5.  
  6. <parent>
  7. <groupId>com.trinityinnovations</groupId>
  8. <artifactId>parent</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. </parent>
  11.  
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15. <java.version>1.8</java.version>
  16. </properties>
  17.  
  18. <dependencies>
  19.  
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-data-jpa</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.restdocs</groupId>
  35. <artifactId>spring-restdocs-mockmvc</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.trinityinnovations</groupId>
  40. <artifactId>frontend</artifactId>
  41. <version>${project.version}</version>
  42. <scope>runtime</scope>
  43. </dependency>
  44. <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
  45. <dependency>
  46. <groupId>commons-httpclient</groupId>
  47. <artifactId>commons-httpclient</artifactId>
  48. <version>3.1</version>
  49. </dependency>
  50. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
  51. <dependency>
  52. <groupId>org.apache.commons</groupId>
  53. <artifactId>commons-csv</artifactId>
  54. <version>1.1</version>
  55. </dependency>
  56. <!-- https://mvnrepository.com/artifact/MysqL/MysqL-connector-java -->
  57. <dependency>
  58. <groupId>MysqL</groupId>
  59. <artifactId>MysqL-connector-java</artifactId>
  60. <version>6.0.6</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>commons-dbcp</groupId>
  64. <artifactId>commons-dbcp</artifactId>
  65. </dependency>
  66. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  67. <dependency>
  68. <groupId>org.hibernate</groupId>
  69. <artifactId>hibernate-core</artifactId>
  70. </dependency>
  71. <dependency>
  72. <groupId>com.fasterxml.jackson.datatype</groupId>
  73. <artifactId>jackson-datatype-hibernate5</artifactId>
  74. </dependency>
  75. <dependency>
  76. <groupId>com.javaetmoi.core</groupId>
  77. <artifactId>javaetmoi-hibernate5-hydrate</artifactId>
  78. <version>2.3</version>
  79. </dependency>
  80. <dependency>
  81. <groupId>com.google.maps</groupId>
  82. <artifactId>google-maps-services</artifactId>
  83. <version>0.1.20</version>
  84. </dependency>
  85. </dependencies>
  86.  
  87. <build>
  88. <plugins>
  89. <plugin>
  90. <groupId>org.springframework.boot</groupId>
  91. <artifactId>spring-boot-maven-plugin</artifactId>
  92. </plugin>
  93. </plugins>
  94. </build>

如果有更多信息需要,请告诉我.

经过大量的搜索后,我遇到了 Maven War Plugin.这使我能够将必要的前端文件拉到后端,以便成功创建我的WAR文件.需要进行的更改如下:

后端pom.xml –
在描述标签添加

  1. <packaging>war</packaging>

然后,在构建标记内部,插件内部添加插件

  1. <plugin>
  2. <artifactId>maven-war-plugin</artifactId>
  3. <configuration>
  4. <webResources>
  5. <resource>
  6. <directory>../frontend/target/frontend</directory>
  7. </resource>
  8. </webResources>
  9. </configuration>
  10. </plugin>

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

还需要在package.json中设置base-href.注意“构建”:

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

猜你在找的Angularjs相关文章