lazybones list
command (or studying the repository )lazybones info < >
command (or read the readme in the packag-e template on Bintree)lazybones create < > < > < >
command lazybones create < > < > < >
groupId
, artifactId
and version via the interactive command linepackage
selected via the interactive command line in the package
and put it in the appropriate directory for the package
(the same for the test, but we will not, for the sake of example)main
, which, when starting the test phase, prints the message selected via the interactive command linemvn test
>lazybones create lazybones-project lzb-templates
│ build.gradle // shalbon build script │ gradlew // nix script startup file │ gradlew.bat // run script file for Windows │ README.md // file describing this project │ ├───gradle // supporting directory for the script │ └───templates // empty directory for our templates
templates
directory, create a sub-directory of our template in it, and start to sculpt. Create a version file. It is called VERSION
and contains only the version, for example 0.1> mkdir maven-simple > cd maven simple > echo 0.1> VERSION
readme.md
, which will be shown after creating the project.src/main/java
, src/main/resources
. In java
and resources
from them we put on an empty .retain
file├───maven-simple │ │ README.md │ │ VERSION │ │ │ └───src │ └──main │ ├──jjava │ │ .retain │ │ │ └───resources │ .retain
<project> <modelVersion>4.0.0</modelVersion> <groupId>${groupId}</groupId> <artifactId>${artifactId}</artifactId> <version>${version}</version> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>${pkg}.App</mainClass> <arguments> <argument>${message}</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
AAAAAH !!!!!!!So, took themselves in hand, look. Pay attention to all sorts of
${...}
. This is what we will change to the values that the user will give us when running create
. In essence, these are just Groovy Templates . If you are familiar with Velocity, Freemarker or any other template handler, everything will be familiar to you. But more about that later.main
App
class. Please note that we are not yet aware of the package
this class and the parameter that we pass to main
.App.java
file: package ${pkg}; public class App { public static void main(String[] args) { System.out.println(args[0]); } }
package
. At the same time, we see that main prints the argument. This means that during the launch of mavena, in the test phase, we expect to see a message that the user selects, again, during the create
.│ App.java │ lazybones.groovy │ pom.xml │ README.md │ VERSION │ ───src └───main ├───java │ .retain │ └───resources .retain
import static org.apache.commons.io.FileUtils.moveFileToDirectory Map<String,String> props = [:] // ask 2 - , . // . ( , ) . props.groupId = ask(' groupId [org.example]: ', 'org.example') props.artifactId = ask(' artifactId [maven-simple]: ', 'maven-simple') props.version = ask(' [1.0-SNAPSHOT]: ', '1.0-SNAPSHOT') props.pkg = ask(" package [$props.groupId]:", props.groupId) props.message = ask(' ? ', ', !') // processTemplates , . processTemplates 'pom.xml', props // String packageDir = props.pkg.replaceAll(/\./, '/') // moveFileToDirectory(new File(targetDir, 'App.java'), new File(targetDir, "src/main/java/$packageDir"), true) // processTemplates 'src/main/java/**/App.java', props
ask()
and processTemplates()
methods and the processTemplates()
field get into the script from the uk.co.cacoethes.lazybones.LazybonesScript
class, which is the custom uk.co.cacoethes.lazybones.LazybonesScript
this script .>gradlew installTemplateMavenSimple
:packageTemplateMavenSimple
:installTemplateMavenSimple
BUILD SUCCESSFUL
>lazybones create maven-simple 0.1 maven-simple
Creating project from template maven-simple 0.1 in 'maven-simple'
groupId [org.example]: com.demo
artifactId [maven-simple]:
[1.0-SNAPSHOT]: 0.1
package [org.example]:org.simple
? , !
-
---------------------------------
. ? .
Project created in maven-simple!
readme.md
. Pay attention, I did not specify artifactId
, I expect maven-simple
by default.│ pom.xml │ README.md │ ───src └───main ├───java │ └──org └───simple │ App.java │ └───resources
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>maven-simple</artifactId> <version>0.1</version> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>org.simple.App</mainClass> <arguments> <argument>, !</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
package org.simple; public class App { public static void main(String[] args) { System.out.println(args[0]); } }
> mvn test [INFO] Scanning for projects ... [INFO] [INFO] ----------------------------------------------- ------------------------- [INFO] Building maven-simple 0.1 [INFO] ----------------------------------------------- ------------------------- [INFO] [INFO] --- maven-resources-plugin: 2.6: resources (default-resources) @ maven-simple --- [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin: 2.5.1: compile (default-compile) @ maven-simple --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin: 2.6: testResources (default-testResources) @ maven-simple --- [INFO] [INFO] --- maven-compiler-plugin: 2.5.1: testCompile (default-testCompile) @ maven-simple --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin: 2.12.4: test (default-test) @ maven-simple --- [INFO] No tests to run. [INFO] [INFO] >>> exec-maven-plugin: 1.2.1: java (default) @ maven-simple >>> [INFO] [INFO] <<< exec-maven-plugin: 1.2.1: java (default) @ maven-simple <<< [INFO] [INFO] --- exec-maven-plugin: 1.2.1: java (default) @ maven-simple --- Hi, Habr! [INFO] ----------------------------------------------- ------------------------- [INFO] BUILD SUCCESS [INFO] ----------------------------------------------- ------------------------- [INFO] Total time: 0.768s [INFO] Finished at: Fri Apr 04 02:54:54 IDT 2014 [INFO] Final Memory: 7M / 304M [INFO] ----------------------------------------------- -------------------------
Source: https://habr.com/ru/post/218205/
All Articles