简体   繁体   中英

Reusing constants and properties in a maven project?

I have a few plugins in pom.xml that need database connection properties. Such us: driver, url, user name, password. All these preferences I already have in persistence.xml file. Can I use them directly, without creating new files?

even if you could, it wont be pretty. you could do it the other way around - specify those parameters as properties in the pom.xml (or a properties file alongside it) and use maven resource filtering to inject them into persistence.xml and any other file that requires them @built time.

so you'd have, in your pom.xml, something like:

<properties>
   <db.driver.class>com.acme.db.JdbcDriver</db.driver.class>
   <db.url>localhost</db.url>
</properties>
...
<build>
   <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
   </resources>
   <plugins>
      <plugin>
         ...
         <configuration>
            <connection>${db.driver.class}/${db.url}</connection>
         </configuration>
      </plugin>
   </plugins>
</build>

and in your persistence.xml you could use ${db.driver.class} and have it replaced by maven during the build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM