Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example documentation for ModuleRef #3763

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/modules/ROOT/pages/fundamentals/modules.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ needing to define your own `Task.Command` in your `build.mill` file

== Aliasing External Modules

include::partial$example/fundamentals/modules/10-external-module-aliases.adoc[]
include::partial$example/fundamentals/modules/10-external-module-aliases.adoc[]

== Abstract Modules References
11 changes: 11 additions & 0 deletions example/fundamentals/modules/11-module-ref/bar/src/bar/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bar;


public class Bar {
public static String generateHtml(String text) {
String value = "<h1>" + text + "</h1>";
return value;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package bar;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class BarTests {

@Test
public void simple() {
String result = Bar.generateHtml("hello");
assertEquals("<h1>hello</h1>", result);
}
}
38 changes: 38 additions & 0 deletions example/fundamentals/modules/11-module-ref/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// When you define an abstract module, often you are referencing an existing module
// somewhere in your build. A naive `def upstreamModule: FooModule`
// would create a module alias, which is often not what you want since the referenced
// module already has a place in your build hierarchy. In such scenarios, you can use
// a `ModuleRef(...)` to wrap the abstract module, such that the abstract `def` does
// not participate in task query resolution:

package build
import mill._, javalib._
import mill.define.ModuleRef

object foo extends JavaModule
object bar extends JavaModule

trait MyTestModule extends JavaModule with TestModule.Junit4{
def upstreamModule: ModuleRef[JavaModule]

def moduleDeps = Seq(upstreamModule())
}

object footest extends MyTestModule{
def upstreamModule = ModuleRef(foo)
}
object bartest extends MyTestModule{
def upstreamModule = ModuleRef(bar)
}


/** Usage

> mill __.test
Test foo.FooTests.simple finished, ...
Test bar.BarTests.simple finished, ...
...

> mill resolve foo.upstreamModule._ # This fails since it's a `ModuleRef`, not just a `Module`
error: resolve Cannot resolve foo.upstreamModule...
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo;


public class Foo {

public static final String VALUE = "hello";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package foo;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class FooTests {

@Test
public void simple() {
assertEquals("hello", Foo.VALUE);
}
}
Loading