在java maven测试之前运行python脚本

时间:2016-03-10 14:00:44

标签: java python maven testing junit

我正在尝试运行一个python脚本,它将生成测试中使用的资源。 python脚本应该在任何单元测试之前运行。

注意:这仅用于测试,生产中不需要所述资源。

我环顾四周,似乎无法让它发挥作用。这就是我在pom.xml中的内容

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <configuration>
    <executable>python</executable>
    <workingDirectory>src/test/resources/</workingDirectory>
  </configuration>
  <executions>
    <execution>
      <id>python-setup</id>
      <phase>test</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <arguments>
          <argument>testSetup.py</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

任何人都可以指出这个设置有什么问题,或者做同样的事情。

我在Mac上运行,我正在通过运行mvn test进行测试。

以下是我运行mvn test时(maven清理后)的输出。

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building app.io 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 21 source files to /Users/steve/app.io/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to /Users/steve/app.io/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ app ---
[INFO] Surefire report directory: /Users/steve/app.io/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

看起来exec-maven-plugin未被调用。

带有exec-maven-plugin标记

-X的输出
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (python-setup)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <arguments>
    <argument>testSetup.py</argument>
  </arguments>
  <basedir default-value="${basedir}"/>
  <classpathScope default-value="runtime">${exec.classpathScope}</classpathScope>
  <commandlineArgs>${exec.args}</commandlineArgs>
  <executable>python</executable>
  <failWithEmptyArgument default-value="true"/>
  <failWithNullKeyOrValueInEnvironment default-value="true"/>
  <longClasspath default-value="false">${exec.longClasspath}</longClasspath>
  <outputFile>${exec.outputFile}</outputFile>
  <project default-value="${project}"/>
  <skip default-value="false">${skip}</skip>
  <sourceRoot>${sourceRoot}</sourceRoot>
  <testSourceRoot>${testSourceRoot}</testSourceRoot>
  <workingDirectory>src/test/resources/</workingDirectory>
  <session default-value="${session}"/>
</configuration>
[DEBUG] =======================================================================
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=2, ConflictMarker.markTime=0, ConflictMarker.nodeCount=239, ConflictIdSorter.graphTime=1, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=97, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=13, ConflictResolver.conflictItemCount=180, DefaultDependencyCollector.collectTime=401, DefaultDependencyCollector.transformTime=20}
[DEBUG] com.elderstudios:app:jar:0.0.1-SNAPSHOT
[DEBUG]    org.springframework.boot:spring-boot-starter-web:jar:1.3.2.RELEASE:compile
[DEBUG]       org.springframework.boot:spring-boot-starter:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot-autoconfigure:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot-starter-logging:jar:1.3.2.RELEASE:compile
[DEBUG]             ch.qos.logback:logback-classic:jar:1.1.3:compile
[DEBUG]                ch.qos.logback:logback-core:jar:1.1.3:compile
[DEBUG]             org.slf4j:jul-to-slf4j:jar:1.7.13:compile
[DEBUG]             org.slf4j:log4j-over-slf4j:jar:1.7.13:compile
[DEBUG]          org.yaml:snakeyaml:jar:1.16:runtime
[DEBUG]       org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.2.RELEASE:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.30:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-validation:jar:1.3.2.RELEASE:compile
[DEBUG]          org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[DEBUG]             javax.validation:validation-api:jar:1.1.0.Final:compile
[DEBUG]             com.fasterxml:classmate:jar:1.1.0:compile
[DEBUG]       com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[DEBUG]          com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile (version managed from 2.6.0 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[DEBUG]       org.springframework:spring-web:jar:4.2.4.RELEASE:compile
[DEBUG]       org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[DEBUG]          org.springframework:spring-expression:jar:4.2.4.RELEASE:compile
[DEBUG]    org.springframework.boot:spring-boot-starter-test:jar:1.3.2.RELEASE:test
[DEBUG]       org.mockito:mockito-core:jar:1.10.19:test
[DEBUG]          org.objenesis:objenesis:jar:2.1:test
[DEBUG]       org.hamcrest:hamcrest-core:jar:1.3:test
[DEBUG]       org.hamcrest:hamcrest-library:jar:1.3:test
[DEBUG]       org.springframework:spring-core:jar:4.2.4.RELEASE:compile
[DEBUG]       org.springframework:spring-test:jar:4.2.4.RELEASE:test
[DEBUG]    org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.2.RELEASE:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-aop:jar:1.3.2.RELEASE:compile
[DEBUG]          org.aspectj:aspectjweaver:jar:1.8.8:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.2.RELEASE:compile
[DEBUG]          org.apache.tomcat:tomcat-jdbc:jar:8.0.30:compile
[DEBUG]             org.apache.tomcat:tomcat-juli:jar:8.0.30:compile
[DEBUG]          org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile
[DEBUG]       org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[DEBUG]          org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile (version managed from 3.1.3.GA by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[DEBUG]          org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[DEBUG]             antlr:antlr:jar:2.7.7:compile
[DEBUG]             org.jboss:jandex:jar:1.1.0.Final:compile
[DEBUG]          dom4j:dom4j:jar:1.6.1:compile
[DEBUG]             xml-apis:xml-apis:jar:1.0.b2:compile
[DEBUG]          org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[DEBUG]          org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[DEBUG]          org.javassist:javassist:jar:3.18.1-GA:compile
[DEBUG]       javax.transaction:javax.transaction-api:jar:1.2:compile
[DEBUG]       org.springframework.data:spring-data-jpa:jar:1.9.2.RELEASE:compile
[DEBUG]          org.springframework.data:spring-data-commons:jar:1.11.2.RELEASE:compile
[DEBUG]          org.springframework:spring-orm:jar:4.2.4.RELEASE:compile (version managed from 4.1.9.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          org.springframework:spring-tx:jar:4.2.4.RELEASE:compile (version managed from 4.1.9.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          org.slf4j:jcl-over-slf4j:jar:1.7.13:compile
[DEBUG]       org.springframework:spring-aspects:jar:4.2.4.RELEASE:compile
[DEBUG]    org.springframework.hateoas:spring-hateoas:jar:0.19.0.RELEASE:compile
[DEBUG]       org.springframework:spring-aop:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          aopalliance:aopalliance:jar:1.0:compile
[DEBUG]       org.springframework:spring-beans:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]       org.springframework:spring-context:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]       org.slf4j:slf4j-api:jar:1.7.13:compile (version managed from 1.7.12 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]    mysql:mysql-connector-java:jar:5.1.38:compile
[DEBUG]    org.json:json:jar:20140107:compile
[DEBUG]    com.google.code.gson:gson:jar:2.3.1:compile
[DEBUG]    javax.interceptor:javax.interceptor-api:jar:1.2:compile
[DEBUG]    com.google.api-client:google-api-client:jar:1.20.0:compile
[DEBUG]       com.google.oauth-client:google-oauth-client:jar:1.20.0:compile
[DEBUG]       com.google.http-client:google-http-client-jackson2:jar:1.20.0:compile
[DEBUG]       com.google.guava:guava-jdk5:jar:13.0:compile
[DEBUG]    com.google.http-client:google-http-client:jar:1.19.0:compile
[DEBUG]       com.google.code.findbugs:jsr305:jar:1.3.9:compile
[DEBUG]       org.apache.httpcomponents:httpclient:jar:4.5.1:compile (version managed from 4.0.1 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          org.apache.httpcomponents:httpcore:jar:4.4.4:compile (version managed from 4.4.3 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          commons-codec:commons-codec:jar:1.9:compile
[DEBUG]    com.google.http-client:google-http-client-gson:jar:1.21.0:compile
[DEBUG]    junit:junit:jar:4.12:test
[DEBUG]    org.assertj:assertj-core:jar:3.2.0:test
[DEBUG]    com.github.springtestdbunit:spring-test-dbunit:jar:1.2.1:test
[DEBUG]    org.dbunit:dbunit:jar:2.5.1:test
[DEBUG]       commons-collections:commons-collections:jar:3.2.2:test (version managed from 3.2.1 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]       org.apache.poi:poi-ooxml:jar:3.11:test
[DEBUG]          org.apache.poi:poi:jar:3.11:test
[DEBUG]          org.apache.poi:poi-ooxml-schemas:jar:3.11:test
[DEBUG]             org.apache.xmlbeans:xmlbeans:jar:2.6.0:test
[DEBUG]                stax:stax-api:jar:1.0.1:test
[DEBUG]    net.javacrumbs.json-unit:json-unit:jar:1.9.0:test
[DEBUG]       net.javacrumbs.json-unit:json-unit-core:jar:1.9.0:test
[DEBUG]    com.jayway.jsonpath:json-path:jar:2.0.0:test
[DEBUG]       net.minidev:json-smart:jar:2.1.1:test
[DEBUG]          net.minidev:asm:jar:1.0.2:test
[DEBUG]             asm:asm:jar:3.3.1:test
[DEBUG]    org.hamcrest:hamcrest-all:jar:1.3:test
[DEBUG]    org.springframework.restdocs:spring-restdocs-mockmvc:jar:1.0.1.RELEASE:test
[DEBUG]       org.springframework.restdocs:spring-restdocs-core:jar:1.0.1.RELEASE:test
[DEBUG]          javax.servlet:javax.servlet-api:jar:3.1.0:test
[INFO] 

更新

我一直在试图偶然发现答案,发现如果我将maven-surefire-plugin更改为不包含任何测试,脚本会执行并按预期工作。当我说不包括测试时,没有测试文件以...结尾... Tests.java

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
            <include>**/*Tests.java</include>  <!-- there is no *Tests.java, it was *Test.java before -->
        </includes>
    </configuration>
</plugin>

1 个答案:

答案 0 :(得分:0)

我认为问题是因为你的maven-exec-plugin是在“测试”阶段调用的。所以它不知道应该先运行什么 - surefire或exec。

尝试将执行移至另一个阶段(我认为最好的是“process-test-resources”):

<executions>
  <execution>
    <id>python-setup</id>
    <phase>process-test-resources</phase>
     <goals>
     <goal>exec</goal>
    </goals>
    <configuration>
      <arguments>
        <argument>testSetup.py</argument>
      </arguments>
    </configuration>
  </execution>