【问题标题】:How to setup angular 4 inside a maven based java war project如何在基于 Maven 的 Java War 项目中设置 Angular 4
【发布时间】:2018-02-05 12:14:01
【问题描述】:

我有点疯狂,因为我找不到在将使用 maven 构建的 java war 项目中设置 angular 4 应用程序的指南。这是因为我想在 Wildfly 服务器中运行它。

有什么帮助吗?

谢谢

【问题讨论】:

标签: java angular maven wildfly war


【解决方案1】:

我有类似的要求,要有一个源项目,它有 java web-services 项目以及 angular 项目(一个基于 angular-cli 的项目),maven build 应该创建一个包含所有 angular 文件的战争。我使用了maven-frontend-plugin,基本路径的配置更改很少。

我们的目标是创建一个war文件,其中包含所有java代码以及war根文件夹中的所有aot编译的角度代码,所有这些都使用单个命令mvn clean package

让所有这些工作的另一件事是避免 angular-app 路由器 url 和您的 java 应用程序 url 之间的冲突,您需要使用 HashLocationStrategy。一种在 app.module.ts 中设置的方法,如下所示

app.module.ts -

providers: [
    { provide: LocationStrategy, useClass: HashLocationStrategy },

]

Angular App 的文件夹结构如下-

角度项目

  • 分布
  • e2e
  • node_modules
  • 公开
    • 应用程序
    • 资产
    • 环境
    • favicon.ico
    • index.html
    • main.ts
    • polyfills.ts
    • style.css
    • tsconfig.json
    • typings.d.ts
    • 等等等等
  • tmp
  • .angular-cli.json
  • .gitignore
  • karma.conf.js
  • package.json
  • README.md
  • tslint.json
  • 等等 - 等等

Maven 项目 -

    • 主要
      • java
      • 资源
      • 网络应用
        • WEB-INF
        • web.xml
  • angular-project(将您的 Angular 项目放在这里
  • node_installation
  • pom.xml

在 pom.xml 中添加 maven-frontend-plugin 配置

 <properties>
    <angular.project.location>angular-project</angular.project.location>
    <angular.project.nodeinstallation>node_installation</angular.project.nodeinstallation>
</properties>

 <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <workingDirectory>${angular.project.location}</workingDirectory>
                <installDirectory>${angular.project.nodeinstallation}</installDirectory>
            </configuration>
            <executions>
                <!-- It will install nodejs and npm -->
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v6.10.0</nodeVersion>
                        <npmVersion>3.10.10</npmVersion>
                    </configuration>
                </execution>

                <!-- It will execute command "npm install" inside "/e2e-angular2" directory -->
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <!-- It will execute command "npm build" inside "/e2e-angular2" directory 
                    to clean and create "/dist" directory -->
                <execution>
                    <id>npm build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Plugin to copy the content of /angular/dist/ directory to output 
            directory (ie/ /target/transactionManager-1.0/) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <id>default-copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <!-- This folder is the folder where your angular files 
                        will be copied to. It must match the resulting war-file name.
                        So if you have customized the name of war-file for ex. as "app.war"
                        then below value should be ${project.build.directory}/app/ 
                        Value given below is as per default war-file name -->
                        <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/${angular.project.location}/dist</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

如上插件在内部调用“npm run build”,确保 package.json 应该在脚本中有 build 命令,如下所示 -

package.json

"scripts": {
    -----//-----,
    "build": "ng build --prod",
   -----//------
}

index.html 应该总是在有人从浏览器中点击应用程序时加载,这就是为什么将其设为欢迎文件的原因。对于 Web 服务,假设我们有路径 /rest-services/* 稍后会解释。

web.xml -

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet-mapping>
    <servlet-name>restservices</servlet-name>
    <url-pattern>/restservices/*</url-pattern>
</servlet-mapping>

如果您的应用程序没有任何上下文路径并且部署在服务器的根路径上,上述配置就足够了。但是,如果您的应用程序有任何上下文路径,例如 http://localhost:8080/myapplication/,那么也要更改 index.html 文件 -

angular-project/src/index.html - 这里的 document.location 将是 myapplication/ (否则是应用程序的上下文路径/如果应用程序没有上下文路径)

使上下文路径成为 angular-app 的基本路径的目的是,每当您从 Angular 进行 ajax http 调用时,它都会将基本路径添加到 url。例如,如果我尝试调用“restservices/persons”,那么它实际上会调用“http://localhost:8080/myapplication/restservices/persons

index.html

 <!doctype html>
 <html lang="en">
 <head>
   <meta charset="utf-8">
   <title>E2E</title>
   <script>document.write('<base href="' + document.location + '" />');     </script>

   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="icon" type="image/x-icon" href="favicon.ico">
 </head>
 <body>
   <app-root></app-root>
 </body>

在您运行mvn clean package 后进行所有上述更改后,它将创建所需的战争。检查 angular 'dist' 文件夹的所有内容是否都在 war 文件的根目录中。

【讨论】:

  • dist 文件夹在这里包含什么?
  • @abhijeet-mohanty,它是构建工件的目标目录,类似于创建最终战争或 jar 的 maven 的目标目录。 Angular cli 将所有打字稿代码转换为 JavaScript 并将其放在 dist 目录中。
  • 我们需要先安装 Angular 吗?
  • @santosh-shelke 这是关于如何在 Java maven 项目中结合 angular cli 项目。您应该首先让 Angular cli 项目本身独立构建和运行良好。如果您可以单独部署 Angular 应用程序和 Java Web 服务应用程序,则不需要上述过程。完成上述设置后,运行 mvn clean package 将构建一个包含 Java + angular 文件的战争。您不需要为此单独安装 node.js 和 angular 包,因为 maven 前端插件会自动安装节点和包。
  • 不要在 node_installation 文件夹中放任何东西。它是前端插件自动下载和安装nodejs的占位符。您可以选择不同的名称,但需要在 pom.xml 中提及相同的名称,以便插件可以了解它。
【解决方案2】:

我有一个类似的问题:我有一个名为 Tourism-Services 的 maven web 应用程序模块,其中包含 web 服务和一个包含 angular 项目的 maven 项目(我在 src/main/angular5/tourism 文件夹中使用 CLI 创建了 angular 项目)

与这篇文章相反,根据 Parth Ghiya 提供的以下链接 (how to integrate Angular 2 + Java Maven Web Application),我认为 angular 项目应该放在 Tourism-Services 模块项目的 webapp 文件夹中。考虑到这一点,我还有其他问题:

  1. 一般情况下,dist 文件夹下的所有编译结果都应该放在 webapp 文件夹下。但是在 dist 文件夹中,并不是所有的 Angular 项目资源(作为图像,css 应该存在于 angular assets 文件夹中,对吗?)

  2. 考虑到 node_modules 中的角度依赖关系,我们是否应该将它们也集成到 webapp 文件夹中?有一个障碍:首先有Typescript文件并且应该也被编译以由服务器解释,但是当它们被导入自定义应用程序角度文件时可能会编译和添加?解决办法是什么?

  3. 我们是否应该在 webapp 文件夹中包含来自 angular 项目的其他类型的资源? (如上述链接帖子中建议的 Scrambo 的类型文件夹)

【讨论】:

  • 我认为您可以将 Angular 项目保留在任何您想要的位置,但在 pom.xml 中提供其正确的相对路径。 1.war插件选择webapp文件夹中的任何内容并将其放在war文件的根目录中。如果您将 dist 的内容放在 webapp 中,那么它将进入战争根目录,在我的回答中,我们不是将 dist 文件夹放在 webapp 中,而是使用 maven-resources-plugin 将 dist 文件夹的内容复制到战争根目录中。两种方式你都会得到相同的结果。 dist 文件夹包含所有依赖项,包括资产文件夹。 2. 正如我所说,dist 文件夹包含所有依赖项,无需集成 node_modules。 3.没必要
  • Web 服务器不会解释 typescript,angular-cli 已经将其转换为 javascript、css。之后,Web 服务器只充当一个 http 服务器,将 javascript 和 css 作为静态内容提供服务。
  • 最后我决定将我的项目分成两部分:第一部分包含 jar 和 war,包含所有后端服务(db manager、rest services 等),第二部分包含一个包含所有前端的角度项目。最后,第一部分部署在 jboss 中,第二部分部署在普通站点(例如 apache)中。我认为这是最好的解决方案
  • @giamma 是的,这是最好的。
【解决方案3】:

我尝试了这些说明和其他文章。 这些很棒,但是有点模棱两可。 所以没有立即得到它的人..检查这个。

GitHub

按照以下说明进行操作..

  1. 在 java 项目下添加您的 Angular 项目。 我的 Angular 项目名称是 tdf

2.open app.module.ts(在你的 Angular 项目/src/app/app.module.ts 中) 添加导入和提供程序

import { LocationStrategy, HashLocationStrategy } from '../../node_modules/@angular/common';

      providers: [
        { provide: LocationStrategy, useClass: HashLocationStrategy },
      ],

3.打开 package.json (angularproject/package.json) 添加“build”:“ng build --prod”,如下所示

{
  "name": "tdf",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
     **"build": "ng build --prod",** //change like this
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

4.更新你的 pom.xml -添加forntend maven插件 -添加 ng 构建目录

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>

          <groupId>angular</groupId>
          <artifactId>angular7java</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>war</packaging>

          <name>angular7java</name>
          <url>http://maven.apache.org</url>

          <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   
            **<angular.project.location>tdf</angular.project.location>**
            <!--your project name -->
            <angular.project.nodeinstallation>node_installation</angular.project.nodeinstallation>
          </properties>

          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
            </dependency>
          </dependencies>

          <build>
           <plugins>    
                <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <version>1.6</version>
                        <configuration>
                        <workingDirectory>${angular.project.location}</workingDirectory>
                        <installDirectory>${angular.project.nodeinstallation}</installDirectory>
                         </configuration>
                        <executions>
                            <execution>
                                <id>install node and npm</id>
                                <goals>
                                    <goal>install-node-and-npm</goal>
                                </goals>
                                <configuration>
                                   <nodeVersion>v9.9.0</nodeVersion>
                                </configuration>
                            </execution>

                            <execution>
                                <id>npm install</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
                                <!-- Optional configuration which provides for running any npm command -->
                                <configuration>
                                    <arguments>install</arguments>
                                </configuration>
                            </execution>

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

                        </executions>
                    </plugin>

                <!-- Plugin to copy the content of /angular/dist/ directory to output 
                    directory (ie/ /target/transactionManager-1.0/) -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.4.2</version>
                    <executions>
                        <execution>
                            <id>default-copy-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <overwrite>true</overwrite>
                                <!-- This folder is the folder where your angular files 
                                will be copied to. It must match the resulting war-file name.
                                So if you have customized the name of war-file for ex. as "app.war"
                                then below value should be ${project.build.directory}/app/ 
                                Value given below is as per default war-file name -->
                                <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${project.basedir}/${angular.project.location}/dist</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
        <!--         <attachClasses>true</attachClasses>
                <webXml>target/web.xml</webXml>
                <webResources>
                    <resource>
                        <directory>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources> -->
            </configuration>
        </plugin>

                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.1</version>
                        <configuration>
                            <fork>true</fork>
                            <executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>

<!--make sure above directory is correct (make it same as your local javac.exe-->
                        </configuration>
                    </plugin>
            </plugins>
        </build>



        </project>

5。 右键单击您的 maven 项目 maven - maven install 要么 终端:mvn clean install

并等到它停止。 安装后,您可以看到 node-installation 文件夹和 war 文件已创建

【讨论】:

  • 我可以在这个java项目中运行任何java业务逻辑吗?
  • @HemanthPoluru 是的,你可以。该项目适用于 java 和 angular 。将您的 java 逻辑放在 /src 中(就像普通的 java 项目一样)。当你得到war文件(在maven安装之后),你会看到你的java也有angular。
  • 我们是否也可以使用其他路由策略来代替哈希。
  • @HemanthPoluru 可以,但是您到底想做什么?有几种方法可以集成 java 和 angular。这个答案是使用 eirslett pugin,但另一种方式是使用另一个库。或者你也可以使用弹簧靴(dzone.com/articles/…)。
  • 我有一个要求,我必须连接到另一个 api,该 api 不接受其中带有“#”的路由。感谢您的快速回复。
猜你喜欢
  • 2021-11-04
  • 2018-11-17
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多