Skip to content

Commit bda67ee

Browse files
committed
samples
1 parent 022b8f4 commit bda67ee

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//// SNIPPET:BUILD
2+
3+
package build
4+
import mill.*, groovylib.*
5+
6+
object foo extends GroovyModule {
7+
def groovyVersion = "5.0.1"
8+
9+
def mvnDeps = Seq(
10+
mvn"org.apache.groovy:groovy-cli-commons", // BOM already loaded by module
11+
mvn"org.apache.groovy:groovy-xml" // BOM already loaded by module
12+
)
13+
14+
def mainClass = Some("foo.Foo")
15+
16+
object test extends GroovyTests with TestModule.Junit5 {
17+
def jupiterVersion = "5.13.4"
18+
19+
def mvnDeps = Seq(
20+
mvn"org.apache.groovy:groovy-test" // BOM already loaded by module
21+
)
22+
}
23+
}
24+
25+
// This is a basic Mill build for a single `GroovyModule`, with one
26+
// third-party dependency and a test suite using the JUnit framework.
27+
//// SNIPPET:TREE
28+
// ----
29+
// build.mill
30+
// foo/
31+
// src/
32+
// foo/Foo.groovy
33+
// resources/
34+
// ...
35+
// test/
36+
// src/
37+
// foo/FooTest.groovy
38+
// out/foo/
39+
// compile.json
40+
// compile.dest/
41+
// ...
42+
// test/
43+
// compile.json
44+
// compile.dest/
45+
// ...
46+
// ----
47+
//
48+
// NOTE: The default Mill source folder layout `foo/src/` differs from that of Maven/Gradle's
49+
// `foo/src/main/groovy`. If you wish to use the Maven source folder layout, e.g. for migrating
50+
// an existing codebase, you should use
51+
// xref:#_maven_compatible_modules[Maven-Compatible Modules]
52+
//
53+
//// SNIPPET:DEPENDENCIES
54+
//
55+
// This example project uses two third-party dependencies
56+
// - Groovy-Cli-Commons for CLI argument parsing
57+
// - Groovy-Xml for HTML templating and escaping
58+
// and uses them to wrap a given input string in HTML templates with proper escaping.
59+
//
60+
// Typical usage of a `GroovyModule` is shown below
61+
62+
/** Usage
63+
64+
> ./mill resolve foo._ # List what tasks are available to run
65+
foo.assembly
66+
...
67+
foo.compile
68+
...
69+
foo.run
70+
...
71+
*/
72+
/** Usage
73+
> ./mill inspect foo.compile # Show documentation and inputs of a task
74+
foo.compile(GroovyModule...)
75+
Compiles all the sources to JVM class files.
76+
Compiles the current module to generate compiled classfiles/bytecode.
77+
Inputs:
78+
foo.allJavaSourceFiles
79+
foo.allGroovySourceFiles
80+
foo.compileClasspath
81+
foo.upstreamCompileOutput
82+
foo.javacOptions
83+
foo.zincReportCachedProblems
84+
...
85+
*/
86+
/** Usage
87+
> ./mill foo.compile # compile sources into classfiles
88+
...
89+
Compiling 1 Groovy sources to...
90+
*/
91+
/** Usage
92+
> ./mill foo.run # run the main method, if any
93+
error: Error: missing option --text
94+
...
95+
*/
96+
/** Usage
97+
> ./mill foo.run --text hello
98+
<h1>hello</h1>
99+
*/
100+
/** Usage
101+
> ./mill foo.test
102+
...
103+
Test foo.FooTest testSimple finished, ...
104+
Test foo.FooTest testEscaping finished, ...
105+
Test foo.FooTest finished, ...
106+
Test run finished: 0 failed, 0 ignored, 2 total, ...
107+
*/
108+
/** Usage
109+
> ./mill foo.assembly # bundle classfiles and libraries into a jar for deployment
110+
111+
> ./mill show foo.assembly # show the output of the assembly task
112+
".../out/foo/assembly.dest/out.jar"
113+
114+
> java -jar ./out/foo/assembly.dest/out.jar --text hello
115+
<h1>hello</h1>
116+
117+
> ./out/foo/assembly.dest/out.jar --text hello # mac/linux
118+
<h1>hello</h1>
119+
120+
> cp ./out/foo/assembly.dest/out.jar out.bat # windows
121+
122+
> ./out.bat --text hello # windows
123+
<h1>hello</h1>
124+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package foo
2+
3+
import groovy.xml.MarkupBuilder
4+
import groovy.cli.commons.CliBuilder
5+
6+
class Foo {
7+
static String generateHtml(String text) {
8+
def writer = new StringWriter()
9+
new MarkupBuilder(writer).h1 {
10+
mkp.yield text
11+
}
12+
writer.toString()
13+
}
14+
15+
static void main(String[] args) {
16+
def cli = new CliBuilder(usage:'help')
17+
cli.t(longOpt:'text', args: 1, 'Passes text to the HTML generation')
18+
def options = cli.parse(args)
19+
20+
if (!options) {
21+
return
22+
}
23+
24+
if (options.h) {
25+
cli.usage()
26+
return
27+
}
28+
29+
String textToProcess = options.t ?: "hello from main"
30+
println generateHtml(textToProcess)
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package foo
2+
3+
import org.junit.jupiter.api.Test
4+
5+
class FooTest {
6+
@Test
7+
void "generate html created properly"() {
8+
assert Foo.generateHtml("hello") == "<h1>hello</h1>"
9+
}
10+
11+
@Test
12+
void "generated html is properly escaped"() {
13+
assert Foo.generateHtml("<hello>") == "<h1>&lt;hello&gt;</h1>"
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// SNIPPET:MAVEN-JAVA-SPOCK
2+
package build
3+
import mill.*, groovylib.*
4+
5+
object `package` extends JavaMavenModuleWithGroovyTests {
6+
7+
object test extends GroovyMavenTests with TestModule.Spock {
8+
def spockVersion = "2.3-groovy-4.0"
9+
def groovyVersion = "4.0.28"
10+
def jupiterVersion = "5.13.4"
11+
}
12+
}
13+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo;
2+
3+
public class Calculator {
4+
5+
public static int add(int a, int b) {
6+
return a + b;
7+
}
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package foo
2+
3+
def "Calculator add: #a + #b = #result"(){
4+
expect:
5+
Calculator.add(a, b) == result
6+
7+
where:
8+
a | b
9+
2 | 2
10+
-2 | -2
11+
result = a + b
12+
}

0 commit comments

Comments
 (0)