建立带有SpringBoot和React子模块的多模块Maven项目

huangapple go评论90阅读模式
英文:

Set up Multi-Module Maven Project with SpringBoot and React Sub-Modules

问题

我正在尝试设置一个包含SpringBoot和React应用程序的monorepo。目前,我的模块结构如下:

  1. root
  2. +-- backend
  3. | +-- pom.xml
  4. +-- frontend
  5. | +-- pom.xml
  6. +-- pom.xml

父级pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>multi-module-demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>pom</packaging>
  10. <modules>
  11. <module>frontend</module>
  12. <module>backend</module>
  13. </modules>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17. </project>

frontend/pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.example</groupId>
  8. <artifactId>multi-module-demo</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. </parent>
  11. <artifactId>frontend</artifactId>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. </properties>
  15. </project>

backend/pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>3.0.2</version>
  10. <relativePath /> <!-- lookup parent from repository -->
  11. </parent>
  12. <artifactId>backend</artifactId>
  13. <properties>
  14. <java.version>17</java.version>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. <maven.compiler.source>17</maven.compiler.source>
  17. <maven.compiler.target>17</maven.compiler.target>
  18. </properties>
  19. <dependencies>
  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-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. </plugin>
  36. </plugins>
  37. </build>
  38. </project>

当查看backend/pom.xml时,我有一个问题,父级不是我的父级pom,而是SpringBoot的pom。然而,这样做的话,我无法管理项目的版本。因为目前构建输出如下:

  1. [INFO] Reactor Summary:
  2. [INFO]
  3. [INFO] multi-module-demo 1.0-SNAPSHOT ..................... SUCCESS [ 0.150 s]
  4. [INFO] frontend 1.0-SNAPSHOT .............................. SUCCESS [ 0.784 s]
  5. [INFO] backend 3.0.2 ...................................... SUCCESS [ 4.102 s]
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] BUILD SUCCESS

您可以看到各个构件的版本不匹配。

我的问题是,我需要如何修改backend/pom以匹配frontend/pom

英文:

I am trying to setup a monorepo containing both a SpringBoot and a React Application. Currently, my module structure looks like the following:

  1. root
  2. +-- backend
  3. | +-- pom.xml
  4. +-- frontend
  5. | +-- pom.xml
  6. +-- pom.xml

Parent pom:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;org.example&lt;/groupId&gt;
  7. &lt;artifactId&gt;multi-module-demo&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;packaging&gt;pom&lt;/packaging&gt;
  10. &lt;modules&gt;
  11. &lt;module&gt;frontend&lt;/module&gt;
  12. &lt;module&gt;backend&lt;/module&gt;
  13. &lt;/modules&gt;
  14. &lt;properties&gt;
  15. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  16. &lt;/properties&gt;
  17. &lt;/project&gt;

frontend/pom:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;parent&gt;
  7. &lt;groupId&gt;org.example&lt;/groupId&gt;
  8. &lt;artifactId&gt;multi-module-demo&lt;/artifactId&gt;
  9. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  10. &lt;/parent&gt;
  11. &lt;artifactId&gt;frontend&lt;/artifactId&gt;
  12. &lt;properties&gt;
  13. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  14. &lt;/properties&gt;
  15. &lt;/project&gt;

and backend/pom:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;parent&gt;
  7. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  8. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  9. &lt;version&gt;3.0.2&lt;/version&gt;
  10. &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
  11. &lt;/parent&gt;
  12. &lt;artifactId&gt;backend&lt;/artifactId&gt;
  13. &lt;properties&gt;
  14. &lt;java.version&gt;17&lt;/java.version&gt;
  15. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  16. &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
  17. &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
  18. &lt;/properties&gt;
  19. &lt;dependencies&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  26. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  27. &lt;scope&gt;test&lt;/scope&gt;
  28. &lt;/dependency&gt;
  29. &lt;/dependencies&gt;
  30. &lt;build&gt;
  31. &lt;plugins&gt;
  32. &lt;plugin&gt;
  33. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  34. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  35. &lt;/plugin&gt;
  36. &lt;/plugins&gt;
  37. &lt;/build&gt;
  38. &lt;/project&gt;

While looking at the backend/pom.xml, I have the issue that the parent is not my parent pom, but the SpringBoot pom. This way however, I could technically not manage e.g. the version of the Project. Because currently the build output is

  1. [INFO] Reactor Summary:
  2. [INFO]
  3. [INFO] multi-module-demo 1.0-SNAPSHOT ..................... SUCCESS [ 0.150 s]
  4. [INFO] frontend 1.0-SNAPSHOT .............................. SUCCESS [ 0.784 s]
  5. [INFO] backend 3.0.2 ...................................... SUCCESS [ 4.102 s]
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] BUILD SUCCESS

where you can see that the versions are not matching of each artifact.

My question is, how do I need to adapt the backend pom to match the frontend pom?

答案1

得分: 1

在进一步调查后,以下是我需要添加到我的父POM文件的内容:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>3.0.2</version>
  7. <scope>import</scope>
  8. <type>pom</type>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
英文:

After some further digging, here's what I need to add to my parent pom:

  1. &lt;dependencyManagement&gt;
  2. &lt;dependencies&gt;
  3. &lt;dependency&gt;
  4. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  5. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  6. &lt;version&gt;3.0.2&lt;/version&gt;
  7. &lt;scope&gt;import&lt;/scope&gt;
  8. &lt;type&gt;pom&lt;/type&gt;
  9. &lt;/dependency&gt;
  10. &lt;/dependencies&gt;
  11. &lt;/dependencyManagement&gt;

huangapple
  • 本文由 发表于 2023年2月10日 05:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定