groovy / gmaven

Groovy integration for Maven
http://groovy.github.io/gmaven/
Apache License 2.0
51 stars 21 forks source link

NPE #9

Closed Sami32 closed 6 years ago

Sami32 commented 6 years ago

While try to use the following configuration Maven throw me a NPE. I probably did a configuration mistake but NullPointerException shouldn't have appeared IMHO.

                         <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <changelogAsVariable>${project.basedir}/releasenotes.md</changelogAsVariable>
                            </properties>
                            <source>
                                def file = new File(project.properties.changelogAsVariable)
                                project.properties.release_description = file.getText('UTF-8')
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Caused by: java.lang.NullPointerException
    at java.io.File.<init> (File.java:277)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance (Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke (CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor (ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor (CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor (AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor (AbstractCallSite.java:190)
    at script1.run (script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate (GroovyShell.java:518)
    at org.codehaus.gmaven.adapter.impl.ScriptExecutorImpl.execute (ScriptExecutorImpl.java:83)
    at org.codehaus.gmaven.plugin.ExecuteMojo.run (ExecuteMojo.java:95)
    at org.codehaus.gmaven.plugin.MojoSupport.execute (MojoSupport.java:47)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
jdillon commented 6 years ago

Configured properties on the execution do not automatically make it into project.properties, you want to use the properties object instead:

<execution>
  <phase>generate-resources</phase>
  <goals>
    <goal>execute</goal>
  </goals>
  <configuration>
    <properties>
      <changelogAsVariable>${project.basedir}/releasenotes.md</changelogAsVariable>
    </properties>
    <source>
      def file = new File(properties.changelogAsVariable) // <--- use properties object not project.properties
      project.properties.release_description = file.getText('UTF-8')
    </source>
  </configuration>
</execution>
Sami32 commented 6 years ago

Thanks a lot, that worked :)

Did you plan to fix it?

jdillon commented 6 years ago

Its not broken so nothing to fix ;-) . per-execution properties should not (and are not meant to) mutate the entire project's properties.

Btw you could just:

<source>
  def file = new File(basedir, 'releasenotes.md')
   project.properties.release_description = file.getText('UTF-8')
</source>

... and avoid using the configuration too.

Also FYI the properties object is a combined view of project.properties, custom properties and overrides.

Sami32 commented 6 years ago

Hehe nice, thank you very much for the advice and knowledge sharing 😄

I admit that this configuration come from the older gmaven-plugin 1.5 ;)

Sami32 commented 6 years ago

Or i misunderstood you or it doesn't work: ( Unrecognised tag: 'source')

            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <source>
                            def file = new File(basedir, 'releasenotes.md')
                            project.properties.release_description = file.getText('UTF-8')
                        </source>
                    </execution>
                </executions>
            </plugin>
jdillon commented 6 years ago

yes, sorry, you still need <configuration>, when I said configuration I mean using the <configuration><properties> element to pass in configuration as a property to the script that is executing, and then dereferencing the property and constructing a file from the string. You can instead just construct the file in the script.

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>groovy-maven-plugin</artifactId>
        <version>2.1</version>
        <executions>
          <execution>
            <phase>generate-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>
                def file = new File(basedir, 'releasenotes.md')
                project.properties.release_description = file.getText('UTF-8')
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>