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

时间:2014-01-09 10:57:15

标签: maven maven-2 maven-3 maven-plugin pom.xml

我无法将.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>

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>

我有一些.properties个文件位于:

src/main/resources
src/main/environment/dev

我无法在 $ {database.usernameee} $ {database.password}

的context.xml中获取值

请说明出了什么问题?

2 个答案:

答案 0 :(得分:5)

执行以下操作:

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>

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

<build>
    <filters>
        <filter>src/main/environment/${env}/some.properties</filter>
    </filters>
    ...

以上假设您的属性文件名为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>

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

(请注意,您可能在'database.usernameee'中输入错字 - 因为它最后有 ee

答案 1 :(得分:1)

如果您想从外部文件(而不是POM内的属性)获取Maven属性,则应使用Maven过滤器:see herehere