java - 为什么 Maven 不包含我的文件/src/main/resources/config.props?

标签 java maven

我认为 src/main/resources 目录中包含的所有内容都会打包到 WAR 中?

我有以下结构:

src/main/resources/META-INF/...
src/main/resources/config.props

包含 META-INF,但不包含 config.props。我如何将其添加到 WAR 中?

<?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>groupId</groupId>
    <artifactId>MyWAR</artifactId>
    <version>1.2</version>

    <packaging>war</packaging>

    <!-- Tell Maven what language version to use -->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api -->
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- Enables the annotations, etc needed -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.exterprise</groupId>
                    <artifactId>cdi-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Our jersey libs -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.25.1</version>
        </dependency>

        <!-- CDI to JAX-RS Binding -->
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers.glassfish/jersey-gf-cdi-ban-custom-hk2-binding -->
        <dependency>
            <groupId>org.glassfish.jersey.containers.glassfish</groupId>
            <artifactId>jersey-gf-cdi</artifactId>
            <version>2.14</version>
        </dependency>

        <!-- Jackson -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.25.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jboss.weld.servlet/weld-servlet-core -->
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet-core</artifactId>
            <version>3.0.0.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jboss.weld/weld-core-impl -->
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-core-impl</artifactId>
            <version>3.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>2.4.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>2.4.2</version>
        </dependency>

    </dependencies>

    <build>

        <!-- We don't use the version number in the file name -->
        <finalName>MyWAR</finalName>

        <plugins>

            <!-- Enhances the JPA classes -->
            <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <execution>
                        <id>JPA Enhance</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                        <configuration>
                            <includes>**/push/*.class</includes>
                            <excludes>**/com/*.class,**/controller/*.class,**/dao/*.class,**/dto/*.class,**/service/*.class,**/util/*.class</excludes>
                            <persistenceXmlFile>${project.basedir}/src/main/resources/META-INF/persistence.xml</persistenceXmlFile>
                            <toolProperties>
                                <property>
                                    <name>addDefaultConstructor</name>
                                    <value>false</value>
                                </property>
                                <property>
                                    <name>enforcePropertyRestrictions</name>
                                    <value>true</value>
                                </property>
                            </toolProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

最佳答案

WAR 文件的根不是资源,而是 src/main/webapp。如果您想要 WAR 文件中直接位于 WEB-INF 下的文件,请将它们放在 src/main/webapp/WEB-INF 中。 src/main/resources 下的资源最终将位于 WAR 文件中的 /WEB-INF/classes 下。

例如,如果您的来源如下所示:

.
|____ src
|  |____ main
|  |  |____ resources
|  |  |  |____ config.props
|  |  |____ webapp
|  |  |  |____ WEB-INF
|  |  |  |  |_____ web.xml
|  |  |  |____ index.jsp

您的 WAR 文件将如下所示:

.
|____ WEB-INF
|  |____ classes
|  |  |____ config.props
|  |____ web.xml
|____ index.jsp

关于java - 为什么 Maven 不包含我的文件/src/main/resources/config.props?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44502346/

相关文章:

java - 用java写入文本文件

java - 如何在 fragment 内使适配器工作到Listview

java - JMeter Beanshell 整数错误

java - 何时添加前提条件以及何时(仅)抛出异常?

java - 无法运行 JAR - 使用 Java 进行 Spark Twitter Streaming

maven - 在运行配置停止之前未连接Intellij IDEA Tomcat应用程序服务器,原因为: Unable to ping server at localhost:1099

java - 用java控制windows

maven - 如何确定需要为 Mule ESB 添加哪些存储库到 Sonatype Nexus

Maven pom : soft vs hard version requirement specification

java - Spring boot父级阻止资源:resources