我变得有点疯狂了,因为我找不到在一个将使用maven构建的java war项目中安装angular 4应用程序的指南。这是因为我想在wildfly服务器上运行它。
有什么帮助吗?
谢谢
发布于 2017-08-29 23:44:34
我有类似的要求,有一个源项目,其中有java web服务项目以及angular项目(一个基于angular-cli的项目),maven build应该创建一个包含所有angular文件的war。我使用了maven-frontend-plugin,对基本路径进行了很少的配置更改。
我们的目标是创建一个war文件,其中包含所有的java代码,外加war根文件夹中所有的aot编译的angular代码,所有这一切都是通过一个命令mvn clean package完成的。
为了避免angular-app路由器urls和您的java应用程序urls之间的冲突,您需要使用HashLocationStrategy。一种方法是在app.module.ts中设置,如下所示
app.module.ts -
providers: [
{ provide: LocationStrategy, useClass: HashLocationStrategy },
]Angular App的文件夹结构如下-
angular-项目
Maven项目-
主
的
放入
将maven-frontend plugin配置添加到pom.xml
<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应该在脚本中有构建命令,如下所示-
package.json
"scripts": {
-----//-----,
"build": "ng build --prod",
-----//------
}当有人从浏览器点击应用程序时,index.html应该总是加载的,这就是为什么它是一个欢迎文件。对于web服务,假设我们有path /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,它将创建所需的war。检查angular 'dist‘文件夹的所有内容是否都在war文件的根目录中。
发布于 2018-09-05 20:57:33
我有一个类似的问题:我有一个名为the services的maven web应用程序模块,其中包含web服务和一个包含angular项目的maven项目(我使用CLI在src/main/angular5/ the文件夹中创建了angular项目)

与这篇文章相反,根据Parth Ghiya提供的以下链接(how to integrate Angular 2 + Java Maven Web Application),我认为angular项目应该放在旅游服务模块项目的webapp文件夹中。考虑到这一点,我有更多的问题:
发布于 2019-02-11 19:29:09
我尝试了这些说明和其他文章。这些都很好,但是有点模棱两可。所以如果一个人不能立刻得到它..检查这个。
请按照以下说明进行操作。
。

2.打开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 -add forntend插件-add 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>一直等到它熄火。安装后,您可以看到节点安装文件夹和war文件已创建

https://stackoverflow.com/questions/45915379
复制相似问题