Search code examples
mavenmaven-cobertura-pluginbuild-helper-maven-plugin

cobertura-maven-plugin and cassandra-maven-plugin caused cassandra.rpcPort to be different


I've got the following configuration in my pom.xml

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>reserve-network-port</id>
                    <goals>
                        <goal>reserve-network-port</goal>
                    </goals>
                    <phase>process-resources</phase>
                    <configuration>
                        <portNames>
                            <portName>cassandra.rpcPort</portName>
                            <portName>cassandra.jmxPort</portName>
                            <portName>cassandra.storagePort</portName>
                            <portName>cassandra.stopPort</portName>
                        </portNames>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <enableAssertions>true</enableAssertions>
                        <excludes>
                            <exclude>none</exclude>
                        </excludes>
                        <includes>
                            <include>**/integration/**/*Test.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cassandra-maven-plugin</artifactId>
            <version>1.2.1-1</version>
            <executions>
                <execution>
                    <id>start-cassandra</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-cassandra</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.1</version>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
        </plugin>

     <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <includes>
                <include>*.*</include>
            </includes>
            <filtering>true</filtering>
        </testResource>
     </testResources>

and properties file have this line cassandra_host=127.0.0.1:${cassandra.rpcPort}

The code uses this property to connect to an instance of cassandra. So whenever I run mvn clean install, cassandra.rpcPort is generated twice, one when the rest is run, and one when cobertura runs. eg.

[INFO] Reserved port 60315 for cassandra.rpcPort
[INFO] Reserved port 60316 for cassandra.jmxPort
[INFO] Reserved port 60317 for cassandra.storagePort
[INFO] Reserved port 60318 for cassandra.stopPort

then when cobertura runs,

[INFO] Reserved port 60319 for cassandra.rpcPort
[INFO] Reserved port 60320 for cassandra.jmxPort
[INFO] Reserved port 60321 for cassandra.storagePort
[INFO] Reserved port 60322 for cassandra.stopPort

And it the cassandra-maven-plugin generates a target/cassandra/conf/cassandra.yaml file which has rpc_port: 60315 (the origin port and not the one from cobertura). However the property file has the value of the port generated by cobertura.

If I disabled cobertura plugin, then everything runs successfully. Anyone know how to get around this issue?

Thanks & regards Tin


Solution

  • OK, I've solved the issue by moving the build-helper-maven-plugin to prepare-package phase, and add the following

    <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <executions>
                        <execution>
                            <id>copy-test-resources</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${project.basedir}/src/test/resources</directory>
                                        <includes>
                                            <include>*.properties</include>
                                        </includes>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    This way, cobertura plugin is already done and the network ports are only scanned/reserved once. Hope this might help someone in the future.