【问题标题】:Spring-Boot-Maven-Plugin - Command to Copy Resources into WAR Main DirectorySpring-Boot-Maven-Plugin - 将资源复制到 WAR 主目录的命令
【发布时间】:2019-05-28 14:34:51
【问题描述】:

问题

在使用Maven 构建的Spring Boot Java 应用程序中,使用spring-boot-maven-pluginWAR packagingEclipse IDEM2,目标为clean package,我想知道如果有一个命令允许我将当前以mywar.war/WEB-INF/classes/static/ 结尾的资源文件夹(cssimages 等)复制到根目录mywar.war 中,这样我就有了mywar.war/images/ 等?

我看到其他插件有类似targetPath 的东西,但我在spring-boot-maven-plugin 的文档中找不到类似的东西。

示例 POM

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <groupId>com.myapp</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0.0</version>
    <name>${project.artifactId}</name>
    <description>My Application</description>

    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
        <tomcat.version>7.0.78</tomcat.version>
        <servlet-api.version>3.0.1</servlet-api.version>
        <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>

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

            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>

        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
        </dependency>

         <dependency>
            <groupId>test</groupId>
            <artifactId>x</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/MyJar.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 你为什么要这么做?
  • 但是你使用的是什么网络框架?
  • 你在哪个目录下有这些文件?
  • 但是一切都会按预期进行。网址将是 ://css 等。
  • 静态目录由 Spring Boot 自动映射为 war 的子目录,或者如果它是独立的 Spring Boot App,则作为根目录。

标签: java maven spring-boot spring-boot-maven-plugin


【解决方案1】:

所以,我最终通过将我的所有资源从/src/main/resources/static 目录移动到/src/main/webapp 目录来解决这个问题。然后所有内容都出现在生成的 WAR 文件的根目录下,Apache HTTP Server 将它们拾取并允许它们通过。

感谢大家的洞察力!

【讨论】:

    【解决方案2】:

    一切都会按预期进行。 Spring Boot 将为您的静态内容提供服务。

    网址将是

    <host>:<port>/<warname>/css 
    <host>:<port>/<warname>/images
    

    等等

    【讨论】:

    • 你好 - 这是真的,但是,这并不能解决 Apache HTTP Server 需要允许访问 mywar.war/WEB-INF/classes/static/ 等处的文件,这样才能在客户端上没有 404 denied 错误的情况下工作-边。我的问题是我需要将文件放在mywar.war/images 以满足要求。
    • 不,你为什么这么认为?你使用 mod _jk 还是 mod_proxy
    猜你喜欢
    • 2017-02-20
    • 2013-10-25
    • 2019-04-17
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多