Skip to content

Commit 3ccf9f2

Browse files
committed
samples
1 parent 022b8f4 commit 3ccf9f2

File tree

23 files changed

+529
-0
lines changed

23 files changed

+529
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// SNIPPET:ALL
2+
// Mill's default folder layout of `foo/src/` and `foo/test/src` differs from that
3+
// of Maven or Gradle's `foo/src/main/groovy/` and `foo/src/test/groovy/`. If you are
4+
// migrating an existing codebase, you can use Mill's `GroovyMavenModule` and
5+
// `GroovyMavenTests` as shown below to preserve filesystem compatibility with an existing
6+
// Maven or Gradle build:
7+
8+
package build
9+
import mill.*, groovylib.*
10+
11+
object foo extends GroovyMavenModule {
12+
13+
def groovyVersion = "5.0.1"
14+
15+
object test extends GroovyMavenTests with TestModule.Junit5 {
16+
}
17+
object integration extends GroovyMavenTests with TestModule.Junit5 {
18+
}
19+
}
20+
21+
// `GroovyMavenModule` is a variant of `GroovyModule`
22+
// that uses the more verbose folder layout of Maven, `sbt`, and other tools:
23+
//
24+
// - `foo/src/main/java`
25+
// - `foo/src/main/groovy`
26+
// - `foo/src/test/java`
27+
// - `foo/src/test/groovy`
28+
// - `foo/src/integration/java`
29+
// - `foo/src/integration/groovy`
30+
//
31+
// Rather than Mill's
32+
//
33+
// - `foo/src`
34+
// - `foo/test/src`
35+
//
36+
// This is especially useful if you are migrating from Maven to Mill (or vice
37+
// versa), during which a particular module may be built using both Maven and
38+
// Mill at the same time
39+
40+
/** Usage
41+
42+
> ./mill foo.compile
43+
Compiling 1 Groovy source...
44+
45+
> ./mill foo.test.compile
46+
Compiling 1 Groovy source...
47+
48+
> ./mill foo.test.testForked
49+
...foo.FooTests hello ...
50+
51+
> ./mill foo.test
52+
...foo.FooTests hello ...
53+
54+
> ./mill foo.integration
55+
...foo.FooIntegrationTests hello ...
56+
57+
*/
58+
59+
// For more details on migrating from other build tools, see xref:migrating/migrating.adoc[]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package foo
2+
3+
import org.junit.jupiter.api.Test
4+
5+
class FooIntegrationTests {
6+
@Test
7+
void "hello should print correct greeting"() {
8+
// Groovy creates an implicit class for the script named after the file
9+
def foo = new Foo()
10+
assert foo.hello() == "Hello World, Earth"
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo
2+
3+
def run(){
4+
println(hello())
5+
}
6+
7+
def hello() {
8+
"Hello World, ${Foo2.VALUE}"
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package foo;
2+
3+
public class Foo2 {
4+
public static final String VALUE = "Earth";
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package foo
2+
3+
import org.junit.jupiter.api.Test
4+
5+
class FooTest {
6+
7+
@Test
8+
void "hello should print correct greeting"() {
9+
// Groovy creates an implicit class for the script named after the file
10+
def foo = new Foo()
11+
assert foo.hello() == "Hello World, Earth"
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bar
2+
3+
def hello() {
4+
"Hello World"
5+
}
6+
7+
def run(){
8+
println new Bar().hello()
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package bar
2+
3+
import org.junit.jupiter.api.Test
4+
import groovy.mock.interceptor.MockFor
5+
6+
class BarTests{
7+
@Test
8+
void "hello"() {
9+
def result = new Bar().hello()
10+
assert result == "Hello World"
11+
}
12+
13+
@Test
14+
void "world"() {
15+
def result = new Bar().hello()
16+
assert result.endsWith("World")
17+
}
18+
19+
@Test
20+
void "using groovy mocks"() {
21+
def mockBar = new MockFor(Bar)
22+
mockBar.demand.hello { "Hello GroovyMock World" }
23+
24+
mockBar.use{
25+
def result = new Bar().hello()
26+
assert result == "Hello GroovyMock World"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)