且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven,在战争即将建成之前运行java方法或脚本

更新时间:2023-09-29 23:11:40

你可以使用 exec-maven-plugin 运行程序/脚本(使用 exec目标)或Java程序(使用 java目标)。



紧接之前的阶段是 prepare-package (参见生命周期参考),所以你可以使用它。但您可能更愿意在生命周期的早期生成web.xml(甚至早在 generate-resources )。



 < 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>

或者,您可以考虑编写自己的插件,特别是如果您认为这个想法对多个项目有用。


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.

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.