0

I'm trying to setup properties in my config.ini which located in src/main/config by using pom.xml as described in this answer, but it doesn't work.

I've placed to src/main/config file config.ini where the following line is present: runtime_mode=${runtime.mode} when I try to access at runtime runtime_mode key I get string ${runtime.mode} instead of DEBUG or PRODUCTION as defined in pom.xml. When I use ${runtime.mode} in *.html file it's being replaced by appropriate value as expected.

So, it doesn't work only when resource located at build path.

I suspect that I define resource in a wrong way, but can't realize what's wrong.

This is build part of my pom.xml:

<project>
    <properties>
        <runtime.mode>DEBUG</runtime.mode>
    </properties>

    <build>
        <finalName>${project.name}</finalName>
        <resources>
          <resource>
            <directory>src/main/config</directory>
            <filtering>true</filtering>
         </resource>
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                          <!-- this is relative to the pom.xml directory -->
                          <directory>src/main/webapp</directory>
                          <filtering>true</filtering>
                          <includes>
                              <include>bower.json</include>
                              <include>index.html</include>
                          </includes>
                        </resource>
                    </webResources>
                </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>
        </plugins>
    </build>
</project>

My app is a war project created by using the following archetype:

<archetype>
      <groupId>com.ibm.tools.archetype</groupId>
      <artifactId>webapp-jee7-liberty</artifactId>
      <version>1.0</version>
      <description>JEE7 web application targeting WebSphere Liberty archetype</description>
</archetype>

UPDATE: Moved my config.ini to src\main\resources and changed my <resource> tag as following:

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>config.ini</include>
            </includes>
        </resource>
    </resources>

But now I can't read any resource located in src/main/resources from code, for example I put there sql.xml where all my sql queries.

Trying read by following code:

private static Properties readProperties(String xmlFileName) throws Exception {
         Properties properties = new Properties();
         InputStream is = SQLProvider.class.getClassLoader().getResourceAsStream(xmlFileName);
         properties.loadFromXML(is);
         return properties;
    }

But it throws an error:

java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203) ~[?:1.8.0_45]
    at java.util.Properties.loadFromXML(Properties.java:881) ~[?:1.8.0_45]
    at ***.SQLProvider.readProperties(SQLProvider.java:26) ~[classes/:?]
Community
  • 1
  • 1
Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • Does your archetype have an archetype.xml or an archetype-metadata.xml in the META-INF directory? – Ahatius Apr 21 '16 at 11:15
  • First: put `config.ini` in `src/main/resources` instead. That folder is specifically for that kind of files. Second: the replacement happens because of the `true` element in the `pom.xml` file, but you have not included `config.ini` in the filtering. That is why the replacement only happens in the `index.html` file. – MichaelK Apr 21 '16 at 11:17
  • @Ahatius, no, it hasn't – Anatoly Apr 21 '16 at 11:37
  • @MichaelKarnerfors, do you mean to add `config.ini` at resource tag? Regarding config vs resources I've found interesting answer here: http://stackoverflow.com/a/4183095/947111 will try to implement it right now – Anatoly Apr 21 '16 at 11:42
  • @MichaelKarnerfors, tried to make a change as you suggested but it doesn't work, I've updated my question. – Anatoly Apr 21 '16 at 12:09
  • Sorry, I'm out of luck there because I am entirely unfamiliar with that archetype. – MichaelK Apr 21 '16 at 12:10

1 Answers1

0

try to use maven-replacer-plugin you can replace any token defined in external file.

Example

      <properties>
         <runtime.mode>DEBUG</runtime.mode>
      </properties>

     <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>maven-replacer-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <ignoreMissingFile>false</ignoreMissingFile>
                <file>path to file/config.ini</file>
                <regex>false</regex>
                <replacements>
                    <replacement>
                        <token>$runtime.mode$</token>
                        <value>${runtime.mode}</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>
Hisham Khalil
  • 1,054
  • 8
  • 9