在Maven JAXB插件中出现“无法创建目录”错误,但只有一次

时间:2019-06-19 14:02:27

标签: java maven jaxb

在我的Maven构建中,我使用jaxb2-maven-plugin:2.2从XML模式文件生成类。这些是项目中唯一的Java文件,即没有未生成的Java文件。

当我在一个干净的项目中运行mvn compile时,遇到以下错误:

[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

但是运行此命令后,target/classes目录实际上确实存在!确实,如果我第二次尝试mvn compile(之间没有进行清理),则构建成功。

最新的插件版本2.4发生相同的错误。

看着引发异常的函数的source,我看到调用了File.mkdirs()。我不知道为什么返回false。毕竟,目录确实在某个时候创建​​了。

为什么会发生这种情况?如何使我的构建在第一次获得成功?

1 个答案:

答案 0 :(得分:0)

所以我可以间接解决这个问题。我已经在Maven中将生成类的目录(非标准目录)标记为源目录。

我对此进行了更改,以便将类生成到默认的target/generated-sources中,并删除了多余的源文件夹。这使错误消失了。