简体   繁体   English

当我在构建时使用配置文件填充属性文件时,如何在 Eclipse 中运行 Maven webapp?

[英]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:这是我的 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:最后我在我的 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 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. Maven 将过滤我的 app.properties 文件,将所有占位符替换为配置文件中保存的值,然后部署填充的属性文件。

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.所有这一切的问题是我喜欢在 Eclipse 中使用服务器机制,我在 IDE 中运行了 Tomcat。 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.我的项目在这种情况下运行,Eclipse 负责更新、重新部署等。Maven 不在图片中,但是,在 Z581D6381F3F35E4F9D77201ACF 中的部署期间,这个属性文件永远不会被正确填充。

So, how can I continue running my project within the IDE, but have this properties file properly populated?那么,我如何才能继续在 IDE 中运行我的项目,但是否正确填充了此属性文件?

You can effectively run any Maven command (including ones with profiles) through the m2eclipse plugin.您可以通过m2eclipse插件有效地运行任何 Maven 命令(包括带有配置文件的命令)。 Also, m2eclipse works with WTP (which I believe is where the Servers tab comes from).此外,m2eclipse 与 WTP 一起使用(我相信这是 Servers 选项卡的来源)。 I'm not certain on this part, but I've used it to deploy web apps to Tomcat within Eclipse for a Maven project. I'm not certain on this part, but I've used it to deploy web apps to Tomcat within Eclipse for a Maven project.

Thanks Alex.谢谢亚历克斯。 I ended up installing Eclipse Integration for Apache Maven (Eclipse IAM), formerly Q for Eclipse我最终安装了Eclipse 集成 Apache Maven(Eclipse IAM),以前是 Q 用于 Z36238378789E494

This plugin solved two problems: populating properties files during Publish to Server events in Eclipse and populating the WEB-INF/lib folder.这个插件解决了两个问题:在 Eclipse 中的 Publish to Server 事件期间填充属性文件和填充 WEB-INF/lib 文件夹。 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.之前,即使我运行 mvn eclipse:eclipse 以满足我在 Eclipse 中的构建路径,它也没有将这些依赖项正确发布到嵌入式服务器。 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.解决了这两个问题后,我认为使用嵌入式服务器在 Eclipse 中开发 Maven 项目没有任何其他障碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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