Is it possible to run a Bash script from Maven?

后端 未结 7 1746
無奈伤痛
無奈伤痛 2021-02-05 01:53

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?

相关标签:
7条回答
  • 2021-02-05 02:34

    Use the maven-antrun-plugin artifact. This way, you can execute several executables sequentially more easily than exec-maven-plugin. Example:

    * The <exec> tag is the important one here.

    <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>
    
    0 讨论(0)
  • 2021-02-05 02:35

    Could the Bash Maven Plugin help you? (Disclaimer: I initiated it, so please send me feedback)

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

    Like Konstantin: When you execute a shell script, you're not portable anymore.

    0 讨论(0)
  • 2021-02-05 02:41

    You can do this, see answer:

    I want to execute shell commands from maven's pom.xml

    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

    0 讨论(0)
  • 2021-02-05 02:43

    To experiment with commands you can use 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
    0 讨论(0)
  • 2021-02-05 02:47

    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:

    echo "call to myFirst.sh, message ${1}"
    
    0 讨论(0)
  • 2021-02-05 02:58

    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 post of 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).

    0 讨论(0)
提交回复
热议问题