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

时间:2010-08-16 09:25:26

标签: 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>

6 个答案:

答案 0 :(得分:129)

以下是为我工作的内容:

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

答案 1 :(得分:32)

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

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

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

答案 2 :(得分:11)

解决。问题是,可执行文件在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>

答案 3 :(得分:1)

2个选项:

  1. 你想要从maven执行一个命令而不绑定到任何阶段,你只需输入命令并且maven运行它,你只是想要运行某些东西,你不在乎如果我们在编译/打包/ ...让我们说我想用maven运行npm start,你可以通过以下方式实现它:
  2. 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时,您可以执行此操作那个:
    2. 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>
      

答案 4 :(得分:1)

这也对我有用。后来,我最终切换到maven-antrun-plugin以避免在Eclipse中发出警告。而且我更喜欢在可能的情况下使用默认插件。示例:

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

答案 5 :(得分:0)

谢谢! Tomer Ben David。它帮助了我。正如我在您提到的npm install那样在演示文件夹中进行pip安装

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