如何使用maven依次执行build和sh



我使用pom.xml的这一部分来执行一个sh文件,该文件将触发我的回归测试。

这部分调用sh文件。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>Regression_Test</id>
            <phase>verify</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>${basedir}/../../qascripting/Vdopia_Automation/exe/hudson_tc_execute.sh</executable>
    </configuration>
</plugin>

当我执行命令mvn clean package时——它只是构建代码,不触发sh,当我使用命令mvn clean验证时——它执行sh但不构建代码。现在我想要一个命令,它应该首先构建代码,然后执行sh.

整个pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vdopia</groupId>
<artifactId>hudson</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<url>http://maven.apache.org</url>
<properties>
    <gson.version>2.2.4</gson.version>
    <guava.version>18.0</guava.version>
    <junit.version>4.10</junit.version>
    <netty.version>4.0.25.Final</netty.version>
    <wurfl.version>1.5.1</wurfl.version>
    <slf4j.version>1.7.7</slf4j.version>
    <redis.version>2.4.2</redis.version>
    <logger.version>0.3.1</logger.version>
    <org.apache.commons.pool.version>1.6</org.apache.commons.pool.version>
    <execplugin.version>1.3.2</execplugin.version>
    <commons.codec.version>1.9</commons.codec.version>
    <geoip.version>1.2.14</geoip.version>
    <org.json.version>20140107</org.json.version>
    <jacoco.version>0.7.2.201409121644</jacoco.version>
    <spymemcached.version>2.11.5</spymemcached.version>
    <aspectjweaver.version>1.8.0</aspectjweaver.version>
    <aspectjrt.version>1.8.0</aspectjrt.version>
    <apacheasynchttpclient.version>4.1</apacheasynchttpclient.version>
    <shadedjar.version>2.3</shadedjar.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <!--plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> 
            <version>${jacoco.version}</version> <executions> <execution> <goals> <goal>prepare-agent</goal> 
            </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> 
            <goals> <goal>report</goal> </goals> </execution> </executions> </plugin -->
        <!-- To download and link source code in eclipse -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>${execplugin.version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${shadedjar.version}</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.vdopia.rtb.netty.Server.HudsonMain</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- REGRESSION TEST -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>Regression_Test</id>
                    <phase>verify</phase>
                    <!-- <phase>generate-test-sources</phase> -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>${basedir}/../../qascripting/Vdopia_Automation/exe/hudson_tc_execute.sh</executable>
            </configuration>
        </plugin>
        <!-- END -->
    </plugins>
</build>
<profiles>
    <profile>
        <id>code-coverage</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<modules>
    <module>utils</module>
    <module>rtb</module>
    <module>adResponse</module>
    <module>DAL</module>
    <module>cache</module>
    <module>redis</module>
    <module>extRequestProcessor</module>
    <module>netty</module>
    <module>ResponseGenerator</module>
    <module>RequestHandler</module>
    <module>Filter</module>
    <module>fluentData</module>
    <module>vast</module>
    <module>externalcache</module>
    <module>templates</module>
    <module>statistics</module>
</modules>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

maven生命周期控制顺序。因此,将sh命令放入验证阶段就可以了。请记住,如果您在较早的阶段遇到了故障,那么maven将停止而不是继续到下一个阶段。这是阶段执行的顺序:https://maven.apache.org/ref/3.3.3/maven-core/lifecycles.html

我组装了一个最小的pom,它工作。这是我的诗

<modelVersion>4.0.0</modelVersion>
<groupId>com.simuquest.qp</groupId>
<artifactId>example1</artifactId>
<version>1.0.13333</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>Regression_Test</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <workingDirectory>${basedir}target</workingDirectory>
                <arguments>
                    <argument>-cp</argument>
                    <argument>${basedir}targetexample1-1.0.13333.jar</argument>
                    <argument>example1.NewMain</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

,下面是我执行"mvn install"

时的输出
    Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

--- maven-jar-plugin:2.3.2:jar (default-jar) @ example1 ---
Building jar: C:UsersAdministratorDocumentsNetBeansProjectsexample1targetexample1-1.0.13333.jar
--- exec-maven-plugin:1.2.1:exec (Regression_Test) @ example1 ---
hello
--- maven-install-plugin:2.3.1:install (default-install) @ example1 ---
Installing C:UsersAdministratorDocumentsNetBeansProjectsexample1targetexample1-1.0.13333.jar to C:UsersAdministrator.m2repositorycomsimuquestqpexample11.0.13333example1-1.0.13333.jar
Installing C:UsersAdministratorDocumentsNetBeansProjectsexample1pom.xml to C:UsersAdministrator.m2repositorycomsimuquestqpexample11.0.13333example1-1.0.13333.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------

所以你现在可以比较工作和不工作的文件。你可以做一个二分搜索,找出你的诗的哪一部分让你悲伤。为什么你不从放弃所有这些插件开始呢?请记住,您只需要在没有错误的情况下进行构建,以便通过各个阶段并到达验证阶段。

最新更新