maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Connolly <stephen.alan.conno...@gmail.com>
Subject Re: How to pass the contents of a file as the value of a command-line parameter when invoking a maven goal
Date Tue, 20 Aug 2013 09:19:42 GMT
[aside]
Why oh why do people always go for the most complex way to solve a
problem... using 10 million plugins when one will do
[/aside]

You were on the right track with this... you just picked a phase of `test`
which is too late in the game as the lifecycle executions will happen
before any executions added by the pom... Pick the port at the `validate`
phase so that it is available for resource filtering...

 <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>validate</phase>
          <configuration>
             <portNames>
                <portName>jetty.proxy.port</portName>
             </portNames>
          </configuration>
       </execution>
    </executions>
 </plugin>

Now at this point you have two choices as to how to pass the port to your
test code.

Option 1. Use resource filtering
Option 2. Use a system property

This all depends on how your tests work....

Option 1

Put the following (you can rename or move as you see fit) as
src/test/resources/ports.properties

jetty.port=${jetty.proxy.port}

Then add the following to your build section

<testResources>
  <testResource>
    <!-- we are defining test resources, so need to add the defaults with
exclusion for filtered resources -->
    <directory>src/testresources</directory>
    <filtering>false</filtering>
    <excludes>
      <exclude>ports.properties</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>src/testresources</directory>
    <filtering>true</filtering>
    <includes>
      <include>ports.properties</include>
    </includes>
  </testResource>
</testResources>

And you are done.

Option 2

Just tell Surefire to pass the system property

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <systemPropertyVariables>
      <jetty.port>${jetty.proxy.port}</jetty.port>
    </systemPropertyVariables>
  </configuration>
</plugin>

Oh and one final thing, when you are writing your tests to read in the port
from either the property file or from the system property, if the value
read is missing or is not a number you should assume a default port... this
is so that IDE integrations will still be able to run the tests without
having to delegate to Maven for building (the modern IDEs know how to do
this... but sometimes they make mistakes).... something you will thank us
for later.... especially once you have test failures to debug.


On 20 August 2013 09:45, thermaleagle <thermaleagle@gmail.com> wrote:

> Hi,
> Thanks for the suggestions. I did try configuring the reserve-network-port
> goal in my pom.xml so that it sets the randomly selected port into the
> property jetty.proxy.port. Then I used the properties maven plugin to write
> this property to the file
> ${project.build.testOutputDirectory}/integration-autogen-maven.properties.
> This seems to work well but fails when the property is being written to the
> file. The error is "Caused by:
> org.apache.maven.plugin.PluginExecutionException: A type incompatibility
> occured while executing
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties:
> java.lang.Long cannot be cast to java.lang.String".
>
> It looks like the properties plugin is failing to convert the reserved port
> 63174 into a String to write to a file, but I am not able to figure out
> why.
>
> The relevant snippet from pom.xml and the error stacktrace from Jenkins is
> appended below for your reference. I am inches close to getting this done
> and a little help from you would make my day.
>
> Thank you!
>
> ------------------------------------------------------------------------------
> ---------------------------------- 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>test</phase>
>                                                 <configuration>
>                                                         <portNames>
>
> <portName>jetty.proxy.port</portName>
>                                                         </portNames>
>                                                 </configuration>
>                                         </execution>
>                                 </executions>
>                         </plugin>
>                         <plugin>
>                                 <groupId>org.codehaus.mojo</groupId>
>
> <artifactId>properties-maven-plugin</artifactId>
>                                 <version>1.0-alpha-2</version>
>                                 <executions>
>                                         <execution>
>                                                 <id>e1</id>
>                                                 <phase>test</phase>
>                                                 <goals>
>
> <goal>set-system-properties</goal>
>                                                 </goals>
>                                                 <configuration>
>                                                         <properties>
>                                                                 <property>
>
> <name>jetty.proxy.port</name>
>
> <value>${jetty.proxy.port}</value>
>                                                                 </property>
>                                                         </properties>
>                                                 </configuration>
>                                         </execution>
>                                         <execution>
>                                                 <id>e2</id>
>                                                 <phase>test</phase>
>                                                 <goals>
>
> <goal>write-project-properties</goal>
>                                                 </goals>
>                                                 <configuration>
>
>
> <outputFile>${project.build.testOutputDirectory}/integration-autogen-maven.properties</outputFile>
>                                                 </configuration>
>                                         </execution>
>                                 </executions>
>                         </plugin>
> ...
> ...
>
>
> ------------------------------------------------------------------------------
> ----------------------------- Jenkins Console Log
> -----------------------------
>
> ------------------------------------------------------------------------------
> [JENKINS] Recording test results
> [INFO]
> [INFO] --- build-helper-maven-plugin:1.8:reserve-network-port
> (reserve-network-port) @ b-c-p ---
> [INFO] Reserved port 63174 for jetty.proxy.port
> [INFO]
> [INFO] --- properties-maven-plugin:1.0-alpha-2:set-system-properties (e1) @
> b-c-p ---
> [INFO] Set 1 system property
> [INFO]
> [INFO] --- properties-maven-plugin:1.0-alpha-2:write-project-properties
> (e2)
> @ b-c-p ---
> [INFO]
> ------------------------------------------------------------------------
> [INFO] BUILD FAILURE
> [INFO]
> ------------------------------------------------------------------------
> [INFO] Total time: 17:00.380s
> [INFO] Finished at: Mon Aug 19 18:15:37 IST 2013
> [INFO] Final Memory: 75M/188M
> [INFO]
> ------------------------------------------------------------------------
>
> [JENKINS] Archiving D:\.(path removed)...pom
>
> Waiting for Jenkins to finish collecting data
>
> mavenExecutionResult exceptions not empty
> message : Failed to execute goal
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties
> (e2) on project b-c-p: A type incompatibility occured while executing
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties:
> java.lang.Long cannot be cast to java.lang.String
> -----------------------------------------------------
> realm =    plugin>org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2
> strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
> urls[0] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/mojo/properties-maven-plugin/1.0-alpha-2/properties-maven-plugin-1.0-alpha-2.jar
> urls[1] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
> Number of foreign imports: 1
> import: Entry[import  from realm
> ClassRealm[project>com.j.r:r-p-pom:1.6.0-SNAPSHOT, parent:
> ClassRealm[maven.api, parent: null]]]
>
> -----------------------------------------------------
>
> cause : A type incompatibility occured while executing
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties:
> java.lang.Long cannot be cast to java.lang.String
> -----------------------------------------------------
> realm =    plugin>org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2
> strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
> urls[0] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/mojo/properties-maven-plugin/1.0-alpha-2/properties-maven-plugin-1.0-alpha-2.jar
> urls[1] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
> Number of foreign imports: 1
> import: Entry[import  from realm
> ClassRealm[project>com.j.r:r-p-pom:13.6.0-SNAPSHOT, parent:
> ClassRealm[maven.api, parent: null]]]
>
> -----------------------------------------------------
>
> Stack trace :
> org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute
> goal
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties
> (e2) on project b-c-p: A type incompatibility occured while executing
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties:
> java.lang.Long cannot be cast to java.lang.String
> -----------------------------------------------------
> realm =    plugin>org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2
> strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
> urls[0] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/mojo/properties-maven-plugin/1.0-alpha-2/properties-maven-plugin-1.0-alpha-2.jar
> urls[1] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
> Number of foreign imports: 1
> import: Entry[import  from realm
> ClassRealm[project>com.j.r:r-p-pom:13.6.0-SNAPSHOT, parent:
> ClassRealm[maven.api, parent: null]]]
>
> -----------------------------------------------------
>
>         at
>
> org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:211)
>         at
>
> org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
>         at
>
> org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:140)
>         at
>
> org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
>         at
>
> org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
>         at
>
> org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
>         at
>
> org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
>         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:316)
>         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:153)
>         at
>
> org.jvnet.hudson.maven3.launcher.Maven3Launcher.main(Maven3Launcher.java:79)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at
>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>         at
>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>         at java.lang.reflect.Method.invoke(Method.java:597)
>         at
>
> org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:329)
>         at
> org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:239)
>         at
> org.jvnet.hudson.maven3.agent.Maven3Main.launch(Maven3Main.java:158)
>         at hudson.maven.Maven3Builder.call(Maven3Builder.java:100)
>         at hudson.maven.Maven3Builder.call(Maven3Builder.java:66)
>         at hudson.remoting.UserRequest.perform(UserRequest.java:118)
>         at hudson.remoting.UserRequest.perform(UserRequest.java:48)
>         at hudson.remoting.Request$2.run(Request.java:326)
>         at
>
> hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
>         at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
>         at java.util.concurrent.FutureTask.run(FutureTask.java:138)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
>         at java.lang.Thread.run(Thread.java:662)
> Caused by: org.apache.maven.plugin.PluginExecutionException: A type
> incompatibility occured while executing
>
> org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties:
> java.lang.Long cannot be cast to java.lang.String
> -----------------------------------------------------
> realm =    plugin>org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2
> strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
> urls[0] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/mojo/properties-maven-plugin/1.0-alpha-2/properties-maven-plugin-1.0-alpha-2.jar
> urls[1] =
>
> file:/D:/_dev/mavenRepo/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
> Number of foreign imports: 1
> import: Entry[import  from realm
> ClassRealm[project>com.j.r:r-p-pom:13.6.0-SNAPSHOT, parent:
> ClassRealm[maven.api, parent: null]]]
>
> -----------------------------------------------------
>
>         at
>
> org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:155)
>         at
>
> org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:195)
>         ... 27 more
> Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to
> java.lang.String
>         at java.util.Properties.store0(Properties.java:812)
>         at java.util.Properties.store(Properties.java:796)
>         at
>
> org.codehaus.mojo.properties.AbstractWritePropertiesMojo.writeProperties(AbstractWritePropertiesMojo.java:61)
>         at
>
> org.codehaus.mojo.properties.WriteProjectProperties.execute(WriteProjectProperties.java:59)
>         at
>
> org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:107)
>         ... 28 more
>
> channel stopped
>
> Finished: FAILURE
>
>
>
>
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/How-to-pass-the-contents-of-a-file-as-the-value-of-a-command-line-parameter-when-invoking-a-maven-gol-tp5767482p5768217.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message