【问题标题】:Make front- and backend (Angular 2 and Spring) Maven Multimodule Project制作前端和后端(Angular 2 和 Spring)Maven 多模块项目
【发布时间】:2017-07-24 10:54:40
【问题描述】:

如何创建一个带有 Spring 后端和 Angular2 前端的 Maven 多模块项目?分别使用 spring initializr (https://start.spring.io) 和 angular cli 似乎很简单,但是我如何将其组织为具有链接但单独的 pom 文件的多模块 Maven 项目?我应该使用什么值以及按什么顺序创建和初始化它们?如果 Intellij IDEA 让事情变得更容易,我可以使用它,但我也可以使用 CMD(我在 Windows 上)。

我在这方面找到的唯一教程在这里:https://blog.jdriven.com/2016/12/angular2-spring-boot-getting-started/ 但是这家伙使用了一些我不想要的自写的“frontend-maven-plugin”。有人可以解释一下这些步骤或将我链接到不使用第三方资源但只清理 Spring 和 Angular2 的教程吗?

编辑:当我发布这个问题时,WebDevelopment 对我来说大部分是新的。下面的解决方案在一开始是可行的,但为了更好的可扩展性,我们决定稍后制作单独的项目:一个包含多个 Angular 应用程序和许多 FE-lib 的 FE 项目(请查看NRWL's NX)。每个 BE-Microservice 一个项目,每个项目都可以在 CI-Pipelines 中单独部署。 Google 自己对所有 FE 和 BE 采用一个项目的方法,但它们确实有特殊要求(所有库必须在最新版本中相互协作)并且他们使用 ABC 堆栈(Angular + Bazel + Closure)堆栈,尚未完全向公众开放,但值得关注:https://github.com/angular/angular/issues/19058

【问题讨论】:

  • 为什么不想使用frontend-maven-plugin?它使事情变得简单、独立并且很好地集成到 Maven 中。将 Angular cli 构建集成到 maven 构建中是完美的。
  • 我创建了空项目并添加了 2 个模块 - spring initializr 和 angular cli。然后我创建了自己的脚本来构建所有内容并将角度应用程序复制到资源/静态/目录中。我想有更好的解决方案,但它对我有用
  • @JBNizet 因为虽然建立一个爱好项目很酷,但我不想稍后在公司项目中使用“一些随机”的代码(即使它是在 apache 许可下)。此外,我不明白该插件到底是做什么的 - 乍一看,我只看到了 FileDownloader 等模块 - 虽然它在未来可能有用,但我更愿意保持它的简约并稍后自己添加所需的模块。
  • 然后自己写一个插件做你想做的事。但请注意,Maven、NodeJS、Java、Spring、Angular、TypeScript 都是随机的开源代码。我不会回答这个插件的作用。 README 中有解释:github.com/eirslett/frontend-maven-plugin
  • @Phil Spring 不关心您在前端使用什么,也不提供构建 Angular 应用程序的工具。这最好由 angular-cli、webpack 等来完成。所以你只需要一些东西来将 angular-cli 构建集成到 maven 构建中。这就是这个插件的重点。

标签: spring maven angular spring-boot angular-cli


【解决方案1】:

构建 Angular 2 应用程序的推荐方法是使用 Angular CLI 工具。同样,当您处理 Java EE 项目时,您通常使用 Maven 作为构建工具。

为了两全其美,您可以按照您的想法开发一个多模块项目。

如果您愿意,只需克隆此示例:

git 克隆https://github.com/prashantpro/ng-jee.git

鉴于前端/UI 项目将是 Angular 2 应用程序。 后端项目可以是 Spring 或纯 Java EE 应用程序(任何 Web 应用程序)。

我们希望获取 Angular 2 输出(dist 目录)并将其映射到 UI 部分的 Web 应用程序项目中。

以下是在没有任何花哨的第三方插件的情况下如何做到这一点。 让我们以这个多模块项目结构为例:

cd ng-jee(这是你的父 POM 项目)

.
├── pom.xml
├── ngdemo
│   ├── pom.xml    --- maven pom for angular module
│   ├── dist       --- This will be Angular output
│   ├── e2e
│   ├── karma.conf.js
│   ├── node_modules
│   ├── package.json
│   ├── protractor.conf.js
│   ├── README.md
│   ├── src
│   ├── tsconfig.json
│   └── tslint.json
└── webdemo
    ├── pom.xml
    └── src

父 pom.xml 需要列出这两个模块。 第一个模块应该是 UI (Angular 2) 模块,然后是 Java/Spring 模块。

重要部分如下所示 ng-jee/pom.xml

<packaging>pom</packaging>
<modules>
    <module>ngdemo</module>
    <module>webdemo</module>
</modules>

接下来,如果您像这样使用 CLI 创建了 Angular 应用程序:

ng 新的 ngdemo

然后你需要在同一个目录中放置一个 pom.xml ngdemo/pom.xml 拥有以下构建插件:(这将构建 Angular CLI 项目并在其 dist 文件夹中生成输出。

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnError>false</failOnError>
                <filesets>
                    <fileset>
                        <directory>.</directory>
                        <includes>
                            <include>dist/**/*.*</include>
                        </includes>
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <id>angular-cli build</id>
                    <configuration>
                        <workingDirectory>.</workingDirectory>
                        <executable>ng</executable>
                        <arguments>
                            <argument>build</argument>
                            <argument>--prod</argument>
                            <argument>--base-href</argument>
                            <argument>"/ngdemo/"</argument>
                        </arguments>
                    </configuration>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最后一个难题是引用这个 ngdemo/dist 文件夹,这样我们就可以将输出复制到我们的 WAR 文件中。

所以,这就是 webdemo/pom.xml

中需要做的事情
<build> 
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <webResources>
                    <resource>
                        <!-- this is relative to the pom.xml directory -->
                        <directory>../ngdemo/dist/</directory>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

现在,如果您从父目录构建项目 ng-jee

mvn clean install

然后你会看到它首先构建 Angular 项目,然后是 Web 项目,在为后者构建构建的同时,它还将 Angular 的 dist 内容复制到 Web 项目的根目录中。

所以你会在 WAR/Web 项目的目标目录中得到类似下面的内容:

/ng-jee/webdemo/target/webdemo-1.0-SNAPSHOT.war

.
├── favicon.ico
├── index.html
├── inline.d72284a6a83444350a39.bundle.js
├── main.e088c8ce83e51568eb21.bundle.js
├── META-INF
├── polyfills.f52c146b4f7d1751829e.bundle.js
├── styles.d41d8cd98f00b204e980.bundle.css
├── vendor.fb5e0f38fc8dca20e0ec.bundle.js
└── WEB-INF
    └── classes

就是这样。我将在这些方面做一个系列,在这里Angular and JEE

在此之前希望这会有所帮助!

【讨论】:

  • 我可以使用这种方法将角度添加到带有 UI 部分的现有 maven 项目吗?
猜你喜欢
  • 2019-08-26
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多