mojohaus / license-maven-plugin

Maven plugin to download and collect license files from project dependencies.
https://www.mojohaus.org/license-maven-plugin/
GNU Lesser General Public License v3.0
107 stars 126 forks source link

StringIndexOutOfBoundsException in v1.12 of plugin #72

Closed harschware closed 7 years ago

harschware commented 7 years ago

For some reason I get index out of bounds exception from plugin. Let me know if there is any additional info I can provdie.

The error came from running mvn license:remove-file-header immediately after successfully running mvn license:update-file-header which changed 27 of my source files. The remove-file-header goal does not remove the header previously applied.

[INFO] --- license-maven-plugin:1.12:remove-file-header (default-cli) @ thinkbig-service-monitor-ambari ---
[INFO] adding extra resolver file:/Users/th186036/IdeaProjects/data-lake-accelerator/plugins/service-monitor-ambari//src/license
[INFO] Will search files to update from root /Users/th186036/IdeaProjects/data-lake-accelerator/plugins/service-monitor-ambari/src
[WARNING] skip failed file : String index out of range: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:658)
    at org.codehaus.mojo.license.RemoveFileHeaderMojo.processFile(RemoveFileHeaderMojo.java:476)
    at org.codehaus.mojo.license.RemoveFileHeaderMojo.processFile(RemoveFileHeaderMojo.java:524)
    at org.codehaus.mojo.license.RemoveFileHeaderMojo.processCommentStyle(RemoveFileHeaderMojo.java:435)
    at org.codehaus.mojo.license.RemoveFileHeaderMojo.doAction(RemoveFileHeaderMojo.java:365)
    at org.codehaus.mojo.license.AbstractLicenseMojo.execute(AbstractLicenseMojo.java:202)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    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)
[INFO] Scan 27 files header done in 64.113ms.
harschware commented 7 years ago

I think this is a critical bug. The issue is that the update-file-header repo doesn't re-apply headers in all cases (I tested changing the organization/name element in the parent pom). The remove-file-header goal will work for some portion of the files and the fail with the exception. Subsequent runs produce the exception without making through any additional headers, so you can't just run it a few times to remove all headers as a work around.

harschware commented 7 years ago

The problem appears to be that it attempts to search past the beginning of the file. To fix, replace lines 476 with:

            try {
                lastchar = content.charAt((--firstIndex));
            } catch ( StringIndexOutOfBoundsException s ) {
                firstIndex = 0;
                break;
            }