bash maven 安装后运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39087922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:04:23  来源:igfitidea点击:

Run a script after maven install

javabashshellmavenmaven-3

提问by Dorin

I have a Maven project and after I install the project I need to run a script. I want to automize this process. My guess is that by adding something in the pom file I could automize this, but so far i haven't found how to run a script after installation. I only found how to run a script before the maven project has finised installing.

我有一个 Maven 项目,安装该项目后我需要运行一个脚本。我想自动化这个过程。我的猜测是,通过在 pom 文件中添加一些内容,我可以自动执行此操作,但到目前为止我还没有找到如何在安装后运行脚本。我只在 maven 项目完成安装之前找到了如何运行脚本。

So, how can I run a script after a Maven project as finished installing?

那么,如何在 Maven 项目安装完成后运行脚本?

回答by Edwin Buck

Use the http://www.mojohaus.org/exec-maven-plugin/exec-maven-plugin, in conjunction with an "executions" configuration block that specifies the installation phase. Make sure it is after your maven-install-plugin as plugins are ran in order (within the same phase)

http://www.mojohaus.org/exec-maven-plugin/exec-maven-plugin与指定安装阶段的“执行”配置块结合使用。确保它在您的 maven-install-plugin 之后,因为插件按顺序运行(在同一阶段)

(in build/plugins)  
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
      <execution>
        <phase>install</phase>
        <goals>
           <goal>exec</goal>
        </goals>
        <configuration>
          <executable>do-something.sh</executable>
          <workingDirectory>/some/dir</workingDirectory>
          <arguments>
             <argument>--debug</argument>
             <argument>with_great_effect</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>

回答by Talon

Why can't you do something like this? This will go after the normal maven install phase.

你为什么不能做这样的事情?这将在正常的 maven 安装阶段之后进行。

EDIT: If you add the maven-install-plugin before it, maven will run each in the order they are in the pom.

编辑:如果您在它之前添加 maven-install-plugin,maven 将按照它们在 pom 中的顺序运行每个。

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
  </plugin>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>install</phase>
        <configuration>
          <tasks>
            <exec
              dir="${project.basedir}"
              executable="${project.basedir}/src/main/sh/do-something.sh"
              failonerror="true">
              <arg line="arg1 arg2 arg3 arg4" />
            </exec>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Source: maven-antrun-plugin

来源:maven-antrun-plugin

回答by Larry McQueary

For a purely maven-driven approach, the answer you're looking for is the execgoal of exec-maven-plugin, and this answer applies: https://stackoverflow.com/a/2008258/3403663

对于纯 maven 驱动的方法,您正在寻找的答案是exec目标exec-maven-plugin,此答案适用:https: //stackoverflow.com/a/2008258/3403663

EDIT: OP indicates the above doesn't work for him.

编辑:OP 表示上述内容对他不起作用。

Alternative approach: I just tried the following in my own project, and it executes lsat the very end of the install phase, after artifacts have been deployed.

替代方法:我刚刚在我自己的项目中尝试了以下方法,它ls在安装阶段的最后执行,在部署工件之后。

mvn clean install exec:exec -Dexec.executable="/bin/ls" -Dexec.args="/etc"

mvn clean install exec:exec -Dexec.executable="/bin/ls" -Dexec.args="/etc"

Otherwise, you could always just wrap the whole thing in a script:

否则,您始终可以将整个内容包装在脚本中:

#!/bin/bash

set -o errexit

mvn clean install
<your other commands here>

回答by chenchuk

You can chain the commands

您可以链接命令

$ mvn clean install && myscript.sh || echo "error."