当前位置: 代码迷 >> java >> 如何使用Maven按顺序执行构建和执行sh
  详细解决方案

如何使用Maven按顺序执行构建和执行sh

热度:24   发布时间:2023-07-25 19:42:33.0

我正在使用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 verify-它执行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会停止而不是前进到下一阶段。 这是阶段执行的顺序: :

我整理了一个最小的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}\target\example1-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:\Users\Administrator\Documents\NetBeansProjects\example1\target\example1-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:\Users\Administrator\Documents\NetBeansProjects\example1\target\example1-1.0.13333.jar to C:\Users\Administrator\.m2\repository\com\simuquest\qp\example1\1.0.13333\example1-1.0.13333.jar
Installing C:\Users\Administrator\Documents\NetBeansProjects\example1\pom.xml to C:\Users\Administrator\.m2\repository\com\simuquest\qp\example1\1.0.13333\example1-1.0.13333.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------

因此,您现在可以比较工作和不工作的pom文件。 您可以进行二进制搜索来找出pom的哪些部分引起了您的悲伤。 为什么不从删除所有这些插件开始。 请记住,您只需构建就可以正确完成阶段并进入验证阶段。