【问题标题】:Maven archetype + Velocity: how to show the current directory and delete some directoryMaven原型+ Velocity:如何显示当前目录和删除一些目录
【发布时间】:2021-04-22 05:23:37
【问题描述】:

因为我想使用原型创建一个多合一的项目,并通过 requiredProperty 删除一些文件和包, 我可以使用 Velocity 通过#if 显示一些代码,如下所示:

#if(${mysql})
interface ${table.mapperName} : ${superMapperClass}<${entity}>
#else

但文件或目录无法删除

我使用代码调用 JDK 方法来获取当前日期:

#set( $ldt = $package.getClass().forName("java.time.LocalDateTime").getMethod("now").invoke(null) )
#set( $dtf = $package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass()).invoke(null, "yyyy/MM/dd HH:mm:ss") )
#set( $date = $ldt.format($dtf))

所以我认为可能可以调用文件 API 来获取当前目录并将其删除:

#set( $CurPath = $package.getClass().forName("java.nio.file.Paths").getMethod("get").invoke(".") )
#set( $CurPathStr = $CurPath.toAbsolutePath().normalize().toString() )
#set( $CurPathStrSys = $package.getClass().forName("java.lang.System").getMethod("getProperty").invoke("user.dir") )

但得到错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate (default-cli) on project standalone-pom: Error merging velocity templates: Invocation of method 'getMethod' in  class java.lang.Class threw exception java.lang.NoSuchMethodException: java.nio.file.Paths.get() at archetype-resources/__rootArtifactId__-service/src/main/java/MainApplication.java[line 5, column 69] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

【问题讨论】:

  • 原型旨在为项目类型创建特定模板...听起来您正在尝试与原型作斗争...
  • 同意@khmarbaise,您最好为每种支持的技术创建一个单独的模板。原型在某种程度上是可定制的,但并不打算无限地如此。
  • 那么会有很多类型原型,但是我想用它来自定义一些依赖和演示代码

标签: maven velocity archetypes


【解决方案1】:

错误是java反射错误,我得到了正确的乐趣:

#set( $RunPath = $package.getClass().forName("java.lang.System").getMethod("getProperty",$package.getClass().forName("java.lang.String")).invoke(null,"user.dir") )
#set( $separator = $package.getClass().forName("java.io.File").getField("separator").get($package.getClass().forName("java.lang.String")))
#set( $packagePath = $package.replace(".",$separator))
#set( $CurPath = $RunPath + $separator + $parentArtifactId + $separator + $artifactId + $separator + "src" + $separator + "main" + $separator + "java" + $separator +  $packagePath + $separator  )

所以它可以通过这个删除一些文件:

#if(${mongo} == "false")
    #set($mongoConfig = $CurPath + "dao" + $separator + "config" + $separator  +  "MongoConfig.java")
    #set($isDelete = $package.getClass().forName("java.io.File").getMethod("delete").invoke($package.getClass().forName("java.io.File").getConstructor($package.getClass().forName("java.lang.String")).newInstance($mongoConfig)))
#end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多