1

我正在开发 grails 2.3.4 应用程序,并且正在尝试添加 maven (3.1.1) 以用于构建目的。我这样做是因为我的项目是模块化的(2 个插件,1 个应用程序 - 现在,以后会有更多插件)并且我想要支持它的工具(我没有使用 Gradle,但这完全是另一个话题)。最近发现了一些我不明白的问题,让我很伤脑筋。对于他们的任何帮助,我将不胜感激。

我的项目结构如下所示:

my_project/
 my_project-domain/ (plugin)
  pom.xml
 my_project-services/ (plugin)
  pom.xml
 my_project-webap/ (app)
  pom.xml
 pom.xml

我的父母 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>my.app.group</groupId>
    <artifactId>myArtifactId</artifactId>
    <packaging>pom</packaging>
    <version>0.0.2</version>

    <name>myArtifactId</name>

    <properties>
        <grails.version>2.3.4</grails.version>
        <h2.version>1.3.170</h2.version>
    </properties>

    <repositories>    
        ...
    </repositories>


    <modules>
        <module>my_project-domain</module>
        <module>my_project-services</module>
        <module>my_project-webapp</module>
     </modules>

     <dependencies>

        <!-- MONGODB -->
        <dependency>
            <groupId>org.grails.plugins</groupId>
            <artifactId>mongodb</artifactId>
            <version>1.3.0</version>
            <scope>compile</scope>
            <type>zip</type>
            <exclusions>
                <!-- proper spring-core and spring-beans version included by org.grails:grails-plugin-services -->
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- TOMCAT needed during testing -->
        <dependency>
            <groupId>org.grails.plugins</groupId>
            <artifactId>tomcat</artifactId>
            <version>7.0.47</version>
            <scope>provided</scope>
            <type>zip</type>
        </dependency>       

        <!-- without h2 TOMCAT throws DataSource errors -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- GRAILS dependencies-->
        <dependency>
            <groupId>org.grails</groupId>
            <artifactId>grails-plugin-async</artifactId>
            <version>${grails.version}</version>
        </dependency>
        ...
    </dependencies>

    <build>
        <pluginManagement/>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-compiler-plugin</artifactId>
                ...
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                ...
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                ...
            </plugin>
            <plugin>
                <groupId>org.grails</groupId>
                <artifactId>grails-maven-plugin</artifactId>
                <version>${grails.version}</version>
                <configuration>
                    <fork>true</fork>
                    <!-- for debugging <forkDebug>true</forkDebug> -->
                </configuration>
                <extensions>true</extensions>
                ...
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
        <id>tools</id>
            ...
        </profile>
    </profiles>
</project>

示例子 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>
    <parent>
        <groupId>my.app.group</groupId>
        <artifactId>myArtifactId</artifactId>
        <version>0.0.2</version>
        <relativePath>..</relativePath>
    </parent>

    <groupId>my.app.group</groupId>
    <artifactId>my_project-services</artifactId>
    <packaging>grails-plugin</packaging>
    <version>0.0.2</version>

    <name>my_project-services</name>
    <description>my_project-services</description>

    <dependencies>
        <dependency>
            <groupId>org.grails.plugins</groupId>
            <artifactId>rest</artifactId>
            <version>0.8</version>
            <scope>runtime</scope>
            <type>zip</type>
        </dependency>
    </dependencies>
</project>

我可以通过以下方式执行集成测试:

  • mvn test -(来自父目录或子目录)-仅执行单元测试
  • grails:test-app - (from child dir) - 执行单元和集成测试
  • mvn grails:test-app - (仅来自子目录)执行单元和集成测试 - 但它执行两次而不是一次(u,i,然后再执行一次 u,i)
  • mvn grails:execute -Dcommand=test-app - (仅从子目录) - 执行适量的单元和集成测试(u,i)但在集成测试期间,其中一些突然启动失败(!!!!)它只是让我大吃一惊为什么..

所以,如果有人试图混合 grails 2.3,也许你有一些关于:

  1. 如何配置 maven 以便能够从父 pom 运行 mvn grails:test-app,而不是这样的结果:

    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Build Order:
    [INFO]
    [INFO] myArtifactId
    [INFO] my_project-domain
    [INFO] my_project-services
    [INFO] my_project-webapp
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building myArtifactId 0.0.2
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- grails-maven-plugin:2.3.4:test-app (default-cli) @ myArtifactId ---
    |Loading Grails 2.3.4
    |Configuring classpath
    |Running pre-compiled script
    .
    |Environment set to test
    .......
    |Application metadata not found, please run: grails upgrade
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO]
    [INFO] myArtifactId ..................................... FAILURE [7.116s]
    [INFO] my_project-domain ................................ SKIPPED
    [INFO] my_project-services .............................. SKIPPED
    [INFO] my_project-webapp ................................ SKIPPED
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    
  2. 为什么通过mvn grails:test-app通过和mvn grails:exec -Dcommand=tests-app失败执行的集成测试以及如何修复它?我需要 exec 命令用于某些特定目的,并且我希望能够依赖它。这是我最大的问题,我完全不明白可能是什么原因

  3. 为什么mvn grails:tests-app会导致双重测试执行?
  4. 为什么我需要为 tomcat 添加 h2 依赖项?在我的数据源中,我只有 mongodb 配置,我只想坚持使用它,并且每次执行集成测试时都不会看到以下错误:

    |Completed 8 unit tests, 0 failed in 0m 2s
    ........pool.ConnectionPool Unable to create initial connections of pool.
    java.sql.SQLException: org.h2.Driver
            at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
            ...
    Caused by: java.lang.ClassNotFoundException: org.h2.Driver
            at org.grails.launcher.RootLoader.findClass(RootLoader.java:147)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at org.grails.launcher.RootLoader.loadClass(RootLoader.java:119)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:249)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
            ... 331 more
    ........
    |Running 4 integration tests...
    

感谢所有到目前为止并且仍在阅读的人 - 无论您是否可以提供帮助:)

汤姆

4

0 回答 0