Maven插件没有在我想要的地方运行

mik*_*keb 3 java properties pom.xml maven

我有一个maven项目,我想在事情发生时从文件加载属性.我有codehaus属性-maven-plugin,我需要它自动运行.

如果我运行mvn testmvn compile,任务插件加载属性文件就好了.

我在输出中看到了这一点: [INFO] --- properties-maven-plugin:1.0-alpha-2:read-project-properties (default)...--- 当我运行时mvn flyway:init,比方说,我没有看到它运行,并且属性没有被加载.

这是pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.foo</groupId>
    <artifactId>workflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Foo Framework</name>
    <description>Foo framework</description>
    <properties>
        <postgre.version>9.1-901.jdbc4</postgre.version>
        <flyway.version>2.2.1</flyway.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgre.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.googlecode.flyway</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <url>${url}</url>
                    <user>${user}</user>
                    <password>${pass}</password>
                    <locations>
                        <location>classpath:sql/pgsql</location>
                    </locations>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgre.version}</version>
                    </dependency>
                </dependencies>
      </plugin>
            <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
                        <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>src/main/resources/db.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我可以做什么,或者我可以阅读一些文档来使这项工作?我已经阅读了大量的maven文档,似乎无法理解它,以实现这一点.我以为我在这里会让它在这个initialize阶段运行,但显然不是......

use*_*849 7

你很亲密 使用阶段运行Maven和使用目标之间存在差异.首先快速回顾一下Maven生命周期文档.

当你运行时mvn compile,Maven会运行绑定到编译阶段的所有目标,以及绑定到早期阶段的所有目标.你已将properties:read-project-properties目标限制在initialize阶段. initialize是第一阶段之一,因此read-project-properties 在命令中使用compiletest之后或任何后续阶段时执行.

当您运行时mvn flyway:init,您只运行一个目标,而不是整个生命周期.因此,使用该命令运行的唯一事情是flyway-maven-plugin的init目标,没有别的.

如果您希望这两个目标一起运行,您可以尝试将flyway:init目标绑定到initialize阶段(如果这不是默认值;我无法通过阅读插件的文档来判断).然后,mvn initialize将运行两个.

如果您不想绑定flyway:init到某个阶段,那么您可以明确地运行这两个目标mvn flyway:init initialize.