Balz Guenat
Balz Guenat

Reputation: 1711

Getting "Unable to create directory" error in Maven JAXB plugin, but only once

In my Maven build, I use the jaxb2-maven-plugin:2.2 to generate classes from an XML schema file. These are the only Java files in the project, i.e. there are no non-generated Java files.

When I run mvn compile with a clean project, I run into the following error:

[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc (xjc) on project awap: Unable to create directory D:\myproject\target\classes\ -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc (xjc) on project awap: Unable to create directory D:\myproject\target\classes\
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    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)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to create directory D:\myproject\target\classes\
    at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.performExecution(AbstractJavaGeneratorMojo.java:482)
    at org.codehaus.mojo.jaxb2.AbstractJaxbMojo.execute(AbstractJaxbMojo.java:257)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 21 more
Caused by: java.io.IOException: Unable to create directory D:\myproject\target\classes\
    at org.codehaus.plexus.util.FileUtils.forceMkdir(FileUtils.java:1615)
    at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.performExecution(AbstractJavaGeneratorMojo.java:439)
    ... 24 more

But after running this, the target/classes directory actually does exist! And indeed, if I attempt mvn compile a second time (without cleaning in between), the build succeeds.

The same error occurs with the most recent plugin version 2.4.

Looking at the source of the function that throws the exception, I see that File.mkdirs() is called. Why this returns false, I don't know. After all, the directory does get created at some point...

Why does this happen and how can I make my build succeed the first time around?

Upvotes: 2

Views: 846

Answers (1)

Balz Guenat
Balz Guenat

Reputation: 1711

So I could indirectly solve this. I had marked the (nonstandard) directory to which the classes were generated as a source directory in maven.

I changed this so the classes get generated into the default target/generated-sources and removed the extra source folder. This made the error go away.

Upvotes: 1

Related Questions