【问题标题】:How to run my maven project (angular+springboot) on tomcat?如何在tomcat上运行我的maven项目(angular+springboot)?
【发布时间】:2019-12-13 23:47:40
【问题描述】:

我创建了一个基于 Angular 8 和 Spring Boot 的 Web 应用程序。我在本地实现了代码库,它工作正常。 我的 Angular 代码(客户端)在 localhost:4200 上运行,spring boot(服务器)在 localhost:8080 上运行。 到目前为止,一切都按预期工作。

现在我想将整个 Web 应用程序部署为一个单独的包,以便我可以将其部署为对 tomcat 的单一战争。

我正在使用 maven 创建战争文件。

但是当我在 tomcat 上部署这个 war 文件并启动 tomcat 时,我无法在浏览器上看到预期的登录页面。

基本上,我对 maven 了解不多,并且正在关注互联网上以下链接中提供的资源来生成战争文件。

https://dzone.com/articles/building-a-web-app-using-spring-boot-angular-6-and

所以我无法确定问题出在我的构建还是我试图通过其访问资源的 URL 上。

如果我只部署 UI 构建,那么如果我点击 localhost:8080,我就能看到登录页面。

我有教程中提到的三个 pom 文件。 1.父母-pom 2. 服务器-pom 3. ui-pom

以下是我的 pom 文件

parent-pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.dzs.licenseGenerator</groupId>
  <artifactId>lg-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <parent>
        <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.6.RELEASE</version>
      <relativePath />
  </parent>
  <modules>
   <module>LicenseGenerator_Backend</module>
    <module>LicenseGenerator_UI</module>
  </modules>
</project>

server-pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>com.dzs.licenseGenerator</groupId>
        <artifactId>lg-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
         <!-- lookup parent from repository -->
    </parent>
    <artifactId>LicenseGenerator_Backend</artifactId>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.dzs.licenseGenerator</groupId>
            <artifactId>LicenseGenerator_UI</artifactId>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                  <execution>
                      <id>copy-resources</id>
                      <phase>validate</phase>
                      <goals><goal>copy-resources</goal></goals>
                      <configuration>
                        <tasks>
                                <echo>Displaying value of pom.xml element</echo>
                                <echo>[project.build.directory] ${project.build.directory}</echo>
                                <echo>[project.parent.basedir] ${project.parent.basedir}</echo>
                            </tasks>
                          <outputDirectory>${project.build.directory}/classes/resources/</outputDirectory >
                          <resources>
                              <resource>
                                  <directory>${project.parent.basedir}/LicenseGenerator_UI/dist/lg-app/</directory >
                              </resource>
                          </resources>
                      </configuration>
                  </execution>
            </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                  <packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
                  <warName>lg-app</warName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

UI-pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dzs.licenseGenerator</groupId>
        <artifactId>lg-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>LicenseGenerator_UI</artifactId>
    <name>LicenseGenerator_UI</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.4</version>

                <configuration>
                    <nodeVersion>v10.16.0</nodeVersion>
                    <npmVersion>6.9.0</npmVersion>
                    <workingDirectory>src/main/web/</workingDirectory>
                </configuration>

                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                    </execution>

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

                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>prod</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run-script build</arguments>
                        </configuration>
                        <phase>generate-resources</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

为了确保我的代码结构是否正确,我在 Eclipse 中发布了我的 Project Explorer 的屏幕截图。

【问题讨论】:

  • 使用 ng build --prod 在生产模式下单独构建您的 Angular 项目。然后你会得到dist package(jar)。在 var/webapps/www/html 中提取你的 dist。然后,您可以通过点击您的主机来访问您的登录信息。
  • 同样的方式,你可以把 spring-boot 的war文件放到tomcat/webapps目录下,而不需要解压war包。
  • 是的,但我希望单战达到目的。其次,为什么要解压dist文件夹?

标签: java angular maven spring-boot tomcat


【解决方案1】:

使用 ng build –prod 命令生成生产构建工件。

在您的 UI 项目文件夹中运行此命令。

生产版本生成后,您应该会看到名为“dist”的新文件夹。

您必须使用 Maven 资源插件来打包为单个 jar。正如我所看到的,您的 pom 中已经有了插件,只需验证目录文件夹以品脱到 dist 文件夹。

之后只需运行 maven clean install 。运行此程序后,您应该会在目标文件夹上看到带有 Angular 6 和 Spring Boot 应用程序的 jar。

使用 Java –jar 命令执行以启动应用程序,您应该会看到从静态文件夹提供的 Angular 应用程序。

【讨论】:

  • 好吧,根据我发布的项目结构,我的 dist 文件夹正在 LicenseGenerator_UI 文件夹中创建。从这里我将它复制到 LicenseGenerator_Backend>target>classes>resources。它应该在静态文件夹中可用吗?
  • 其次,正如您在 LicenseGenerator_Backend 中看到的那样>目标 2 war 文件正在生成 lg-app.war 和 LicenseGenerator_Backend-0.0.1-SNAPSHOT.war。前一场战争不包含角度构建,但后一场战争包含。任何线索为什么会发生这种情况。我曾尝试部署两场战争,但没有运气。
  • @NaveenYadav ,无需复制,Spring Boot 应用程序中的 maven 资源插件将为您完成。在您的 maven-war-plugin 中,您提到了另一个战争名称,这就是生成 2 场战争的原因。保持你的插件如下: org.apache.maven.pluginsmaven-war-plugin
【解决方案2】:

您可以使用frontend-maven-plugin 开始您的前端构建。前端构建完成后,会在dist 目录下生成资源文件。

之后,您可以使用maven-resources-plugin 将文件从dist 复制到target 目录中的所需位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2011-12-05
    • 2020-10-30
    • 2010-10-29
    • 2013-07-22
    • 2019-03-26
    • 2019-03-30
    相关资源
    最近更新 更多