Conditionally include exclude files while creating project using maven archetype

前端 未结 3 498
温柔的废话
温柔的废话 2021-02-04 10:06

Am trying to create an archetype which will conditionally include file based on the user input.

Like for eg., if a user will use this custom archetype and pass pa

3条回答
  •  攒了一身酷
    2021-02-04 10:47

    The answer above by GreyBeardedGeek is the correct one. In case someone needs an example of how that Groovy script should look like, I wrote a small blogpost.

    Here's the Groovy script from my post:

    import java.nio.file.Files
    import java.nio.file.Path
    import java.nio.file.Paths
    
    // the path where the project got generated
    Path projectPath = Paths.get(request.outputDirectory, request.artifactId)
    
    // the properties available to the archetype
    Properties properties = request.properties
    
    // connectionType is either ftp or sftp
    String connectionType = properties.get("connectionType")
    
    // the Java package of the generated project, e.g. com.acme
    String packageName = properties.get("package")
    
    // convert it into a path, e.g. com/acme
    String packagePath = packageName.replace(".", "/")
    
    if (connectionType == "sftp") {
      // delete the FTP file
      Files.deleteIfExists projectPath.resolve("src/main/java/" + packagePath + "/polling/FtpFlowBuilder.java")
    } else if (connectionType == "ftp") {
      // delete the SFTP file
      Files.deleteIfExists projectPath.resolve("src/main/java/" + packagePath + "/polling/SftpFlowBuilder.java")
    }
    
    

提交回复
热议问题