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

Nar*_*mar 99 maven-2

我想用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>
Run Code Online (Sandbox Code Playgroud)

Cur*_*tis 141

这是为我工作的:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 我想补充一点,这不适用于(至少)插件的1.5.0版本,因为`<configuration />`应该在`<executions />`之后,而不是放在其中.我花了很长时间才发现这个简单的语法错误.Maven的错误输出真的没那么有用. (11认同)
  • Maven阶段列于此处:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference (3认同)
  • 在配置块中使用`<workingDirectory> $ {basedir}/scripts/</ workingDirectory>`,而不是在`<executable>`中给出完整路径也可能是一个好主意 (2认同)

Pas*_*ent 34

这里的问题是我不知道预期的是什么.使用当前设置,在命令行上调用插件将起作用:

$ 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>
Run Code Online (Sandbox Code Playgroud)

然后:

$ 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] ------------------------------------------------------------------------
...

请注意,您可以在configuration内部指定execution.

  • @ gnash-85:这很正常.当您在父级上调用`mvn exec:exec`时,mvn将在多模块构建的所有项目(包括父级)上运行它.但是父pom没有任何配置,因为插件需要定义`executable`,因此错误消息. (5认同)
  • @ gnash-85:我仍然不知道你在做什么.我使用了你提供的确切片段**并没有问题,如上所示.请更新您的问题,以显示您如何调用maven(以及您更改内容时当前的配置). (2认同)
  • 加上这种妄想.如果在`execution`块中指定`configuration`,它将作为组的一部分运行(`mvn install`),但如果直接使用`mvn exec运行,则抛出`指定以下内容:<configuration>`错误:exec`. (2认同)

小智 15

解决了.问题是,可执行文件在Linux中以不同的方式工作.如果你想运行.sh文件,你应该写下面的方式.把它写在pom.xml中

    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!对于我作为一个喜欢 shell 脚本的 Windows 用户来说,这是关键! (2认同)

Tom*_*vid 6

2 选项:

  1. 你想从 maven 执行一个命令而不绑定到任何阶段,你只需输入命令然后 maven 运行它,你只想让 maven 运行一些东西,你不在乎我们是否在 compile/package/... 让我们说我想npm start用 maven运行,你可以用下面的方法实现它:

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>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)