没有从context.xml文件中的属性文件获取值

use*_*668 1 maven-2 maven-plugin pom.xml maven-3 maven

我无法从.properties文件中检索到我的值context.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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <groupId>com</groupId>
    <artifactId>myproj</artifactId>
    <version>0.1.1</version>
    <packaging>war</packaging>

    <name>myproj</name>
    <url>http://maven.apache.org</url>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
                <build.number>0</build.number>
                <svn.revision.number>0</svn.revision.number>
            </properties>
        </profile>

    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>

            </resource>
            <resource>
                <directory>src/main/environment</directory>
                <filtering>false</filtering>

            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <printSummary>false</printSummary>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <excludes>
                        <exclude>**/*_Roo_*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                    <additionalBuildcommands>
                        <buildCommand>
                            <name>org.eclipse.ajdt.core.ajbuilder</name>
                            <arguments>
                                <aspectPath>org.springframework.aspects</aspectPath>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>org.springframework.ide.eclipse.core.springbuilder</name>
                        </buildCommand>
                    </additionalBuildcommands>
                    <additionalProjectnatures>
                        <projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
                        <projectnature>com.springsource.sts.roo.core.nature</projectnature>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warName>myproj</warName>
                </configuration>
            </plugin>
        </plugins>
    </build>



</project>
Run Code Online (Sandbox Code Playgroud)

context.xml(在src/main/webapp/META-INF中)

<?xml version="1.0" encoding="UTF-8"?>

<Context>
 <!-- Specify a JDBC datasource -->
<Resource name="jdbc/mydb" auth="Container"
  type="javax.sql.DataSource" username="${database.usernameee}" password="${database.password}"
  driverClassName="net.sourceforge.jtds.jdbc.Driver"
  url="jdbc:jtds:sybase://localhost:9000/common_db"
 maxActive="10" maxIdle="4" />
</Context>
Run Code Online (Sandbox Code Playgroud)

我在.properties里面有一些文件:

src/main/resources
src/main/environment/dev
Run Code Online (Sandbox Code Playgroud)

我无法在$ {database.usernameee}$ {database.password}的 context.xml中获取值

请说明出了什么问题?

Wil*_*ing 6

请执行下列操作:

1)删除该<resources>部分.您不想进行过滤,这src/main/resources是默认设置 - 不需要单独指定.

<build>
    <!-- Delete the <resources> section -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>

        </resource>
        <resource>
            <directory>src/main/environment</directory>
            <filtering>false</filtering>

        </resource>
    </resources>
Run Code Online (Sandbox Code Playgroud)

2)<filters><build>指定环境特定属性文件下添加元素

<build>
    <filters>
        <filter>src/main/environment/${env}/some.properties</filter>
    </filters>
    ...
Run Code Online (Sandbox Code Playgroud)

以上假设您的属性文件被调用some.properties- 因此将其更改为文件的真实名称.

3)修改配置 maven-war-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/META-INF/context.xml</include>
                </includes>
            </resource>
        </webResources>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
        <warName>myproj</warName>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在,这将过滤context.xml使用特定于环境的属性 - 具体取决于您使用的配置文件.

(请注意,'database.usernameee'中可能有拼写错误 - 因为它最后有额外的ee)