2014-01-09 81 views
0

我無法從.properties文件中檢索值到我context.xml沒有獲得在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和 $ 的context.xml {} database.password內值

請問哪裏出了問題?

+0

誰能告訴我什麼地方出了錯請? – user3128668

+0

你想把'database.username'和'database.password'屬性從相關的* .properties替換到'context.xml'中? –

+0

@WillKeeling YEP!我更新了我的代碼位以上,使爲假,並刪除標記,即使那麼iam仍然沒有從屬性文件中獲取context.xml中的值.. – user3128668

回答

4

執行以下操作:

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末)

+0

哇,非常感謝!但我有一個疑問:下面的標籤是做什麼的? 的src /主/ web應用 **/META-INF/context.xml的 user3128668

+0

我沒有理解如何以下標記對我很有用: src/main/webapp src/main/webapp/WEB -INF/web.xml user3128668

+0

''內的''標記告訴maven war插件過濾src/main/webapp'文件夾,但在進行過濾時只包含'context.xml'。是的,您可以安全地刪除''和'',因爲這些只是重申默認值。 –

1

如果你想從外部文件(不是來自POM內部的屬性)獲得Maven屬性,你應該使用Maven Filters:see herehere

+0

Thanq! :),可以請你回答我的查詢乙),在這裏:http://stackoverflow.com/questions/21037291/difference-between-keeping-a-plugin-inside-a-project-and-inside-a-profile – user3128668

相關問題