为什么maven在构建过程中不复制属性文件?

时间:2023-01-14 09:12:23

Nothing I've found has been able to help me solve this one specific case. I recently switched from a plain old java web app project (which was working) to a maven web project. I get the following runtime exception:

我发现的任何东西都无法帮助我解决这个具体案例。我最近从一个普通的旧的Java Web应用程序项目(正在运行)切换到一个maven web项目。我得到以下运行时异常:

java.util.MissingResourceException: Can't find bundle for base name com.myapp.config, locale en

I am using Netbeans to create a JSF 2.0, Spring, and Hibernate web app. I have the following directory structure:

我正在使用Netbeans创建一个JSF 2.0,Spring和Hibernate Web应用程序。我有以下目录结构:

src\main\java\com\myapp     Contains config.properties
src\main\resources               Empty

target\myapp\WEB-INF\classes\com\myapp     Contains compiled class files without config.properties
src\main\java\com\myapp                                 Contains config.properties

src \ main \ java \ com \ myapp包含config.properties src \ main \ resources空目标\ myapp \ WEB-INF \ classes \ com \ myapp包含没有config.properties的编译类文件src \ main \ java \ com \ myapp包含config.properties

Inspection of the WAR file in the target folder does not show any sign of the properties file so it's as if the Maven build plug-in is not copying over properties files. I know there is a tag you can place inside the pom but it didn't work for me. The link below mentions that the resources folder (empty for me) has its contents included during the build but if that is the case, how do you do it from Netbeans? I just want the properties file to be packaged with my war so it is accessible when it is deployed to the server.

http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

检查目标文件夹中的WAR文件不会显示属性文件的任何迹象,因此就好像Maven构建插件没有复制属性文件一样。我知道有一个标签可以放在pom里面,但它对我不起作用。下面的链接提到资源文件夹(对我来说是空的)在构建期间包含其内容但如果是这种情况,你如何从Netbeans做到这一点?我只是希望将属性文件与我的war一起打包,以便在将其部署到服务器时可以访问它。 http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

pom.xml:

pom.xml中:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<repositories>
    <repository>
        <id>java.net</id>
        <name>Repository hosting the Java EE 6 artifacts</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.1.8</version>
    </dependency>
    <dependency>
        <groupId>net.authorize</groupId>
        <artifactId>java-anet-sdk</artifactId>
        <version>1.4.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.15</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${artifactId}</finalName>
</build>
<profiles>
    <profile>
        <id>endorsed</id>
        <activation>
            <property>
                <name>sun.boot.class.path</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- javaee6 contains upgrades of APIs contained within the JDK itself.
                             As such these need to be placed on the bootclasspath, rather than classpath of the
                             compiler.
                             If you don't make use of these new updated API, you can delete the profile.
                             On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
                        <compilerArguments>
                            <bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
                        </compilerArguments>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>javax</groupId>
                            <artifactId>javaee-endorsed-api</artifactId>
                            <version>6.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<properties>
    <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
</properties>

3 个解决方案

#1


12  

What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.

您的项目的构建路径配置为在Netbeans中是什么?您可以尝试将其更改为src / main / webapp / WEB-INF / classes。这样,从src / main / java文件夹编译的类文件以及src / main / resources下的任何资源都应该包含在生成的WAR中。如果将它放在src / main / resources文件夹下,则可以访问config.properties文件。

You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).

您还可以查看pom.xml中的任何包含部分,并确保不会意外地排除某些内容(如果您明确包含某些内容,则可能会隐式排除其他内容)。

#2


36  

Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:

默认情况下,Maven不会从java源代码树复制资源,但您可以通过将其添加到pom.xml来实现:

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <excludes><exclude>**/*.java</exclude></excludes>
    </resource>
  </resources>
</build>

Make sure you exclude the java source files.

确保排除java源文件。

From http://www.ninthavenue.com.au/how-to-change-mavens-default-resource-folder

来自http://www.ninthavenue.com.au/how-to-change-mavens-default-resource-folder

#3


0  

Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.

尝试将config.properties放在src \ main \ resources \ com \ myapp下。我能够在本地项目上测试这个。我正在运行Maven 3.0.2。

Created a mvn sample project with the webapp archetype:

使用webapp原型创建了一个mvn示例项目:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

Created a directory at src/main/resources/com/foo and put a foo.properties file under it.

在src / main / resources / com / foo创建了一个目录,并在其下放置了一个foo.properties文件。

Ran a build:

跑一个版本:

mvn clean install

Then, when looking in the resulting target directory, the foo.properties file appears:

然后,查看生成的目标目录时,将显示foo.properties文件:

ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r--  1 sblaes  staff    4 Apr  2 22:09 foo.properties

You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.

您可以在计算机上尝试这些步骤。如果可行,那么开始尝试通过从中删除内容来简化上面的POM,看它是否开始工作。试验和错误并不好玩,但我只是没有看到任何应该破坏它的东西。

#1


12  

What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.

您的项目的构建路径配置为在Netbeans中是什么?您可以尝试将其更改为src / main / webapp / WEB-INF / classes。这样,从src / main / java文件夹编译的类文件以及src / main / resources下的任何资源都应该包含在生成的WAR中。如果将它放在src / main / resources文件夹下,则可以访问config.properties文件。

You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).

您还可以查看pom.xml中的任何包含部分,并确保不会意外地排除某些内容(如果您明确包含某些内容,则可能会隐式排除其他内容)。

#2


36  

Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:

默认情况下,Maven不会从java源代码树复制资源,但您可以通过将其添加到pom.xml来实现:

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <excludes><exclude>**/*.java</exclude></excludes>
    </resource>
  </resources>
</build>

Make sure you exclude the java source files.

确保排除java源文件。

From http://www.ninthavenue.com.au/how-to-change-mavens-default-resource-folder

来自http://www.ninthavenue.com.au/how-to-change-mavens-default-resource-folder

#3


0  

Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.

尝试将config.properties放在src \ main \ resources \ com \ myapp下。我能够在本地项目上测试这个。我正在运行Maven 3.0.2。

Created a mvn sample project with the webapp archetype:

使用webapp原型创建了一个mvn示例项目:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

Created a directory at src/main/resources/com/foo and put a foo.properties file under it.

在src / main / resources / com / foo创建了一个目录,并在其下放置了一个foo.properties文件。

Ran a build:

跑一个版本:

mvn clean install

Then, when looking in the resulting target directory, the foo.properties file appears:

然后,查看生成的目标目录时,将显示foo.properties文件:

ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r--  1 sblaes  staff    4 Apr  2 22:09 foo.properties

You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.

您可以在计算机上尝试这些步骤。如果可行,那么开始尝试通过从中删除内容来简化上面的POM,看它是否开始工作。试验和错误并不好玩,但我只是没有看到任何应该破坏它的东西。