Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a Maven webapp in Eclipse when I use profiles to populate properties files at build time?

Here is an example profile in my POM:

    <profiles>
    <profile>
        <id>QA</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <jdbc.url>jdbc:mysql://127.0.0.1:3306/SomeDB</jdbc.url>
            <jdbc.username>webapp</jdbc.username>
            <jdbc.password>somepassword</jdbc.password>
        </properties>
    </profile>
    ...

I then have a properties file in my resources folder like this:

jdbc.url = ${jdbc.url}
jdbc.username = ${jdbc.username}
jdbc.password = ${jdbc.password}

and finally I turn filtering on in my POM:

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

This is actually a simplified example, but you get the idea. Basically when I run

mvn install -P QA

Maven will filter my app.properties file, replace all the placeholders with the values held in the profile, and deploy the populated properties file.

The problem with all of this is that I like to utilize the Servers mechanism in Eclipse, where I have Tomcat running within the IDE. My projects runs in this instance, and Eclipse takes care of updating, redeploying, etc. Maven is left out of the picture, however, during deployments within the IDE and this properties file never gets populated properly.

So, how can I continue running my project within the IDE, but have this properties file properly populated?

like image 264
Robert Campbell Avatar asked Mar 21 '09 17:03

Robert Campbell


People also ask

How do I run a Maven build in Eclipse?

Building and Running the Maven Project in Eclipse To run the maven project, select it and go to “Run As > Java Application”. In the next window, select the main class to execute. In this case, select the App class and click on the Ok button. You will see the “Hello World” output in the Console window.

Which of the following is achieved through Maven profiles?

Profiles modify the POM at build time, and are used to give parameters different target environments (for example, the path of the database server in the development, testing, and production environments).

Where are profiles configured in Maven?

The profiles/dev/config. properties file contains the configuration that is used in the development environment. The profiles/prod/config. properties file contains the configuration that is used in the production environment.


2 Answers

You can effectively run any Maven command (including ones with profiles) through the m2eclipse plugin. Also, m2eclipse works with WTP (which I believe is where the Servers tab comes from). I'm not certain on this part, but I've used it to deploy web apps to Tomcat within Eclipse for a Maven project.

like image 56
Alex Miller Avatar answered Sep 28 '22 02:09

Alex Miller


Thanks Alex. I ended up installing Eclipse Integration for Apache Maven (Eclipse IAM), formerly Q for Eclipse

This plugin solved two problems: populating properties files during Publish to Server events in Eclipse and populating the WEB-INF/lib folder. Before, even though I was running mvn eclipse:eclipse to satisfy my Build Path in Eclipse, it was not publishing these dependencies to the embedded servers correctly. This plugin does that. Having solved these two issues, I don't see any other barriers to developing a Maven project in Eclipse using the embedded servers.

like image 26
Robert Campbell Avatar answered Sep 28 '22 01:09

Robert Campbell