代码之家  ›  专栏  ›  技术社区  ›  Giamma

如何在基于maven的java war项目中设置angular 4

  •  29
  • Giamma  · 技术社区  · 7 年前

    有什么帮助吗?

    谢谢

    3 回复  |  直到 6 年前
        1
  •  40
  •   TimeTraveler    6 年前

    我有类似的需求,需要一个源项目,其中包含java web services项目和angular项目(基于angular cli的项目),maven build应该创建一个包含所有angular文件的war。我用过 maven-frontend-plugin 基本路径的配置更改很少。

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

    应用程序。单元输电系统-

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

    Angular应用程序的文件夹结构如下-

    角度投影

    • e2e
    • node_模块
    • 平民的
      • 应用程序
      • 环境
      • 法维孔。ico公司
      • 指数html
      • 多边形填充。输电系统
      • 打字。d、 输电系统
    • .角度cli。json
    • .gitignore
    • 包裹json
    • etc-etc

    • src公司
        • Java语言
        • 资源
        • 网络应用程序
          • 网状物xml
    • 将角度投影放在此处 )
    • node_安装

    将maven前端插件配置添加到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>
    

    包裹json

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

    网状物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/

    角度项目/src/索引。html-此处为文档。位置将是myapplication/(应用程序的上下文路径,否则/如果应用程序没有上下文路径)

    使上下文路径成为angular应用程序的基本路径的目的是,无论何时从angular发出ajax http调用,它都会将基本路径前置到url。例如,如果我尝试调用“restservices/persons”,那么它实际上会调用' http://localhost:8080/myapplication/restservices/persons '

    指数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清洁包

        2
  •  0
  •   Bence Kaulics    6 年前

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

    enter image description here

    与此相反,并相应地与以下链接( how to integrate Angular 2 + Java Maven Web Application )这是Parth Ghiya给出的,我认为angular项目应该放在旅游服务模块项目的webapp文件夹中。考虑到这一点,我还有其他问题:

    1. 考虑到node_模块中的角度依赖性,我们是否也应该将它们集成到webapp文件夹中?有一个障碍:首先是Typescript文件,还应该编译以供服务器解释,但在将它们导入自定义应用程序角度文件时,可能会编译并添加它们?解决方案是什么?

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

        3
  •  0
  •   Eric Aya    6 年前

    我试过那些说明和其他文章。 这些都很好,但是有点模糊。

    GitHub

    按照以下说明操作。。

    1. 在java项目下添加angular项目。 tdf公司 project structure

    2.打开应用程序。单元ts(在您的angular project/src/app/app.module.ts中) 添加导入和提供程序

    import { LocationStrategy, HashLocationStrategy } from '../../node_modules/@angular/common';
    
          providers: [
            { provide: LocationStrategy, useClass: HashLocationStrategy },
          ],
    

    {
      "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 -添加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>
    

    右键单击maven项目maven-maven安装 端子:mvn清洁安装

    等它停下来。 安装后,您可以看到创建了节点安装文件夹和war文件 enter image description here