我想从 Maven 的 pom.xml 中执行 shell 命令

我想用 Maven 执行 Linux shell 命令:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>hostname</executable>
</configuration>
</plugin>
196767 次浏览

以下是我一直在做的事情:

<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution><!-- Run our version calculation script -->
<id>Version Calculation</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/scripts/calculate-version.sh</executable>
</configuration>
</execution>
</executions>
</plugin>

这里的问题是我不知道什么是 意料之中。在当前的设置中,在命令行上调用插件可以正常工作:

$ mvn exec:exec
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3491937
[INFO]    task-segment: [exec:exec]
[INFO] ------------------------------------------------------------------------
[INFO] [exec:exec {execution: default-cli}]
[INFO] laptop
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

使用全局 configuration,执行 hostname命令(laptop是我的主机名)。换句话说,这个插件可以按预期工作。

现在,如果你想让一个插件执行 作为建造的一部分,你必须在 绑定的一个特定阶段的目标。例如,要将其绑定到 compile上:

  <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>some-execution</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>hostname</executable>
</configuration>
</plugin>

然后:

$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3491937
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/Q3491937/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [exec:exec {execution: some-execution}]
[INFO] laptop
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

注意,可以在 execution中指定 configuration

解决了。问题是,可执行文件在 Linux 中以不同的方式工作。如果要运行 .sh文件,应该将 Exec-maven-plugin添加到 pom.xml文件的 <plugins>部分。

<plugin>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<!-- Run our version calculation script -->
<id>Renaming build artifacts</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<commandlineArgs>handleResultJars.sh</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>

2个选择:

  1. 你想要执行一个来自 maven 的命令而不需要绑定到任何阶段,你只需要输入命令并且 maven 运行它 ,你只需要 maven 运行一些东西,你不需要关心我们是否在编译/包/... 让我们假设我想用 maven 运行 npm start,你可以用下面的方法来实现:

mvn exec:exec -Pstart-node

为此,您需要下面的 maven 部分

  <profiles>
<profile>
<id>start-node</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>npm</executable>
<arguments><argument>start</argument></arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>


</profiles>
  1. 你想要 在特定阶段从 maven 运行任意命令,例如,当我在安装阶段,我想运行 npm install,你可以这样做:

mvn install

为了达到这个目的,你需要以下部分:

  <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>


<execution>
<id>npm install (initialize)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>initialize</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>

谢谢!Tomer Ben David.帮助了我。因为我正在做演示文件夹中的 pip 安装,正如你提到的 npm 安装

<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>pip</executable>
<arguments><argument>install</argument></arguments>
<workingDirectory>${project.build.directory}/Demo</workingDirectory>
</configuration>

这招对我也管用。后来,为了避免 Eclipse 中的警告,我最终切换到了 maven-antrun-plugin。如果可能的话,我更喜欢使用默认插件。例如:

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>get-the-hostname</id>
<phase>package</phase>
<configuration>
<target>
<exec executable="bash">
<arg value="-c"/>
<arg value="hostname"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

接受的答案在2021年版本3.0.0中不起作用。execution标记中的 configuration标记产生一条消息 < strong > “‘ Exectuable’参数丢失或无效。”

解决方案是将 configuration标记移到 executions标记之外。

<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<id>Version Calculation</id>
<executable>${basedir}/scripts/calculate-version.sh</executable>
</configuration>
</plugin>