Maven Profiles integration with parameters in pom

Hello,

I created a pom with several profiles in, one profile containing parameters (username and password) that need to be supplied. If I run maven command line with -Duser=blah -Dsecret=blah, it seems to work. However, I don't find a consistent way of configuring IntelliJ to use these parameters.

I select the Profile in the Maven Projects view, and I tried to supply those parameters in all kinds of ways on the junit configuration but it's not being picked up. What seems to work i creating a Run Configuration for maven on the Maven Projects tab, for example compile, and supplying the parameters to that configuration. Then each time I launch that maven compile configuration, the next junit runs will pick the parameters up, it seems. That is to say, as long as the JUnit Run Configuration existed before I ran the Maven Run Configuration. For a new test I need to rerun the maven configuration, if I relaunch the editor I need to do so, and so on. Is there any consistent way of supplying parameters to the maven profile in the JUnit configuration (I would like to have it in the JUnit configuration, must be possible because there's clearly a link between the selected maven profile and the junit configuration, different profiles yield clearly different results, it just seems to not want to work with parameters in the profile). Is this a bug in the editor or one in my head?

Kasper

7 comments

Hello.

Can you show your profile definition in the pom.xml?

Regards,
Alexander.

0

Something like this. This does work on maven command line with -Duser=... -Dsecret=...


  <profile>
        <id>profile_id</id>
        <properties>
            <user>${user}</user>
            <secret>${secret}</secret>
            <proxyHost>hostname</proxyHost>
            <proxyPort>hostport</proxyPort>
            <environment>local</environment>
        </properties>
    </profile>

0

Thank you.

How do you use these properties? Something like System.getProperty() in java code, or anything else?

(Sample project is welcome, if this is convenient to you.)

Regards,
Alexander.

0

I can't really send you my current project (they wouldn't like it at work :-p), and to make a small sample project seems a bit too much trouble right now (if you tell me it 's impossible for you to find out what's going on/reproduce, I'll do it, no probs, but I guess not).
Anyways, I use these properties to let maven fill out a properties file which I have put under src/main/resources, called env.properties, which looks like this :

user=${user}
password=${secret}
proxyHost=${proxyHost}
proxyPort=${proxyPort}
environment=${environment}

These same properties are also present in the profiles in the pom. This properties file is then read in through this little slice of code and injected all over the place through Guice.

       Properties properties = new Properties();
        InputStream resourceAsStream = getClass().getResourceAsStream("/env.properties");
        InputStreamReader reader= new InputStreamReader(resourceAsStream);
        properties.load(reader);

Now, the thing is, this works perfectly command line and IntelliJ when the profiles themselves have no placeholders, so if I have a profile

  <profile>
        <id>kasper-test</id>
        <properties>
            <user>suske</user>
            <secret>antigoon</secret>
            <proxyHost>10.0.3.212</proxyHost>
            <proxyPort>7070</proxyPort>
            <environment>TEST</environment>
        </properties>
    </profile>

As soon as the profile contains placeholders for user and secret, like so

   <profile>
        <id>id</id>
        <properties>
            <user>${user}</user>
            <secret>${secret}</secret>
            <proxyHost>other-server</proxyHost>
            <proxyPort>80</proxyPort>
            <environment>other_env</environment>
        </properties>
    </profile>

It works when I invoke maven command line with mvn clean test -Pid -Duser=suske -Dsecret=antigoon, but in IntelliJ I can't seem to get it working, I can't seem to get the placeholders filled out. So when I load up the properties file in Guice, I can see that other-server, other-env, and so on are correctly replaced in the env.properties file, but user is still ${user} and secert is still ${secret}. Might very well be that I missed something, that I didn't fill out the placeholders correctly in the IDE, but I tried a bit of everything and nothing seems to work consistently.

Let me know if I need to make you a small sample project in a github or so, I will not be able to take out time for that immediately though.

0

Thanks a lot. Seems that I need just one more piece of information:

> I use these properties to let maven fill out a properties file

How do you get it? Via filtering with maven-resources-plugin, like here: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html or in some different way?

Alexander.

0

Indeed, through

   <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <version>2.3</version>
              <configuration>
                  <encoding>UTF-8</encoding>
              </configuration>
          </plugin>

and

   <resources>
          <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
          </resource>
      </resources>

0

Thank you for your patience.

AFAIS, the core problem is that non-Maven run configurations (like JUnit) are generic, they knows nothing about Maven. While to make the resources filtered we need to pass Maven profile id. So we need Maven profile setting in "generic" JUnit and alike.

If so, this looks like a known problem: http://youtrack.jetbrains.com/issue/IDEA-87143
Please follow.

Regards,
Alexander.

0

Please sign in to leave a comment.