Maven, run a java method or script before war is a

2019-09-05 23:57发布

I am thinking of using a template engine to generate the web.xml and other things.

Is there as way to to run a java file or a script before the maven install command? Or before the war is generated.

I am not sure what the phase should be, but basically before anyone else looks at the web.xml so I can touch it to make a new valid one.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-06 00:59

You can use the exec-maven-plugin to run either a program/script (using the exec goal) or a Java program (using the java goal).

The phase immediately before package is prepare-package (see the Default lifecycle in the Lifecycle Reference), so you could use that. But you might prefer to generate the web.xml earlier in the lifecycle (even as early as generate-resources).

Putting these together, you might try something like this:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <executable>your_packaging_script</executable>
      <!-- optional -->
      <workingDirectory>/tmp</workingDirectory>
      <arguments>
        <argument>--some-option</argument>
      </arguments>
    </configuration>
  </plugin>

Alternatively, you might consider writing your own plugin, especially if you think the idea would be useful for more than one project.

查看更多
登录 后发表回答