是否可以从 Maven 运行 bash 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8433652/
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
Is it possible to run a bash script from Maven?
提问by user710818
For creating configuration of my application I need to run bash script. Is it possible to integrate execution of bash scripts in Maven, maybe there are some plugins? Thanks.
为了创建我的应用程序的配置,我需要运行 bash 脚本。是否可以在 Maven 中集成 bash 脚本的执行,也许有一些插件?谢谢。
采纳答案by Konstantin Pribluda
You can do this, see answer:
您可以这样做,请参阅答案:
I want to execute shell commands from maven's pom.xml
我想从 maven 的 pom.xml 执行 shell 命令
But it is not advisable, as this produces not so portable builds. Why do you need this in first place? Using this plugin usually indicates some weird necessity in project build
但这是不可取的,因为这会产生不那么便携的构建。为什么首先需要这个?使用这个插件通常表明在项目构建中有一些奇怪的必要性
回答by Adrien
Could the Bash Maven Pluginhelp you? (Disclaimer: I initiated it, so please send me feedback)
可以在猛砸Maven插件帮助你吗?(免责声明:我发起了它,所以请给我反馈)
<build>
<plugins>
<plugin>
<!-- Run with:
mvn bash:run
mvn install
-->
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>bash-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>test</id>
<phase>integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<script>
# Here you can execute shell commands
echo "Tomcat will start"
/opt/apache-tomcat/bin/startup.sh
</script>
</configuration>
</plugin>
</plugins>
</build>
You will need to install this maven plugin in your own Maven repo.
你需要在你自己的 Maven 仓库中安装这个 Maven 插件。
Like Konstantin: When you execute a shell script, you're not portable anymore.
像 Konstantin:当你执行一个 shell 脚本时,你不再是可移植的。
回答by Frans uit Almelo
Would look more like:
看起来更像:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>generateSources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec executable="/bin/bash">
<arg value="myFirst.sh" />
<arg value="inputOne" />
</exec>
<exec executable="/bin/bash">
<arg value="src/mySecond.sh" />
<arg value="inputTwo" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
With myFirst.sh:
使用 myFirst.sh:
echo "call to myFirst.sh, message "
回答by james25
Solved. The problem is, executable is working in a different way for bash. This code is working. Write it in pom.xml
解决了。问题是,可执行文件对 bash 的工作方式不同。此代码正在工作。写在 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>
回答by Jared
If at all possible, I'd recommend using a scripting language that runs inside the JVM with dependencies that are captured either in your project or in the maven repository. That way your builds are platform independent and your dependencies are captured (i.e. you don't loose the build machine and realize your bash script was specific to that box). I showed an example in this postof using jacl. There are also good examples of using javascript and groovy inside antrun (though there may be more straightforward ways of calling them directly).
如果可能的话,我建议使用在 JVM 内运行的脚本语言,并在您的项目或 maven 存储库中捕获依赖项。这样你的构建是平台独立的并且你的依赖被捕获(即你不会丢失构建机器并意识到你的 bash 脚本是特定于那个盒子的)。我在这篇文章中展示了一个使用 jacl 的例子。在 antrun 中也有使用 javascript 和 groovy 的好例子(尽管可能有更直接的方法可以直接调用它们)。
回答by AlikElzin-kilaka
Use the maven-antrun-plugin
artifact. This way, you can execute several executables sequentially more easily than exec-maven-plugin
. Example:
使用maven-antrun-plugin
神器。这样,您可以比exec-maven-plugin
. 例子:
* The <exec>
tag is the important one here.
*<exec>
标签在这里很重要。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec executable="my1.sh">
<arg value="input1"/>
</exec>
<exec executable="my2.sh">
<arg value="input2"/>
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
回答by Alexey
To experiment with commands you can use exec:exec
:
要试验您可以使用的命令exec:exec
:
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="your arguments"
your arguments
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="'your arguments'"
your arguments
This demonstrates:
这表明:
- passing arguments: if you need to pass several arguments: just split them with space
- if you need to pass an argument with spaces: enclose it in quotes, as you would do in bash script/terminal
-q
to shut up the mvn log
- 传递参数:如果你需要传递几个参数:只需用空格分割它们
- 如果您需要传递带空格的参数:将其括在引号中,就像您在 bash 脚本/终端中所做的那样
-q
关闭 mvn 日志