无法从context.xml文件中的属性文件获取值 [英] Not getting values from properties files in context.xml file

查看:106
本文介绍了无法从context.xml文件中的属性文件获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将.properties文件中的值检索到我的context.xml

I am unable to retrieve value from .properties file into my 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内部)

context.xml (inside 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文件:

I have some .properties files inside under:

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

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

I am unable to get values inside my context.xml of ${database.usernameee} and ${database.password}

请指出出了什么问题?

推荐答案

执行以下操作:

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

1) Remove the <resources> section. It's not needed for the filtering you want to do and src/main/resources is the default - it doesn't need to specified separately.

<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)在<build>下添加一个<filters>元素,指定特定于环境的属性文件

2) Add a <filters> element under <build> specifying the environment specific properties file

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

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

The above assumes your properties files are called some.properties - so change this to the real name of your file.

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-取决于您使用的配置文件.

That will now filter the context.xml using the environment specific properties - depending upon what profile you used.

(请注意,您在'database.usernameee'中可能有错字-因为结尾处有额外的 ee )

(note that you may have a typo in 'database.usernameee' - as it has extra ee at the end)

这篇关于无法从context.xml文件中的属性文件获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆