在maven项目中添加groovy库

时间:2014-06-19 16:06:45

标签: java maven groovy

我是Groovy的新手,我正在尝试使用Groovy编写一个非常简单的项目作为语言。我能够编写一个hello world脚本,部署一个mvn工件并执行它。 我现在已经超越了这一点,我需要导入一些外部库来测试问题,但我不知道该怎么做。

如果你能给我一些关于此的提示,我将非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以为具有test范围的项目指定依赖关系,在这种情况下,它们不会在compile范围内使用,或者您可以指定仅在某些情况下使用的依赖关系(如果,例如,您希望编写Groovy测试而无需团队的其他成员这样做)。

此示例pom.xml具有自定义profile元素,该元素描述仅在当前环境用户为" andylopresto"时才运行的依赖项和执行。这意味着如果我的团队不想在他们的构建中使用Groovy,我仍然可以在Groovy中编写我的测试类,并让它们在我的机器上作为标准mvn test phase的一部分运行只要。在我的团队成员'本地机器(或共享的Jenkins实例),Groovy测试文件将被忽略(未编译或运行,groovy-all依赖项甚至不会下载到机器上)。

示例:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>your.package</groupId>
<artifactId>artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>
<developers>
    <developer>
        <id>alopresto</id>
        <name>Andy LoPresto</name>
        <email>alopresto@example.com</email>
        <roles>
            <role>developer</role>
            <role>owner</role>
        </roles>
    </developer>
    <developer>
        <id>jjava</id>
        <name>Johnny Java</name>
        <email>ihategroovy@example.com</email>
        <roles>
            <role>sticking to older languages</role>
        </roles>
    </developer>
</developers>
<dependencyManagement>
    <dependencies>
         <!-- This dependency is available to all project code -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <!-- This dependency is only available during test phase -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- This dependency is expected to be provided outside of Maven -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util</artifactId>
            <version>8.0.8</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<profiles>
    <profile>
        <!-- Custom profile for Groovy tests used only on Andy's computer -->
        <id>andyLocalTest</id>
        <activation>
            <property>
                <name>env.USER</name>
                <value>andylopresto</value>
            </property>
        </activation>

        <dependencies>
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>2.2.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <id>add-source</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/main/groovy</source>
                                </sources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>add-test-source</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/test/groovy</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <compilerId>groovy-eclipse-compiler</compilerId>
                        <source>1.6</source>
                        <target>1.6</target>
                        <includes>
                            <include>**/*.java</include>
                            <include>**/*.groovy</include>
                        </includes>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-compiler</artifactId>
                            <version>2.8.0-01</version>
                        </dependency>
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-batch</artifactId>
                            <version>2.1.5-03</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>