Part 3: Adding dependencies
Let's imagine now that in our simple app, we now want to add a programming language dependency. Here's how our build might look like as a result
Go
JavaScript
Java
Python
./go.mod
1
module github.com/earthly/earthly/examples/go
2
​
3
go 1.13
4
​
5
require github.com/sirupsen/logrus v1.5.0
Copied!
./go.sum (empty)
1
​
Copied!
The code of the app might look like this
./main.go
1
package main
2
​
3
import "github.com/sirupsen/logrus"
4
​
5
func main() {
6
logrus.Info("hello world")
7
}
Copied!
The build then might become
./Earthfile
1
FROM golang:1.15-alpine3.13
2
WORKDIR /go-example
3
​
4
build:
5
COPY go.mod go.sum .
6
COPY main.go .
7
RUN go build -o build/go-example main.go
8
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
9
​
10
docker:
11
COPY +build/go-example .
12
ENTRYPOINT ["/go-example/go-example"]
13
SAVE IMAGE go-example:latest
Copied!

Note

To copy the files for this example ( Part 3 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part3/part3 ./part3
Copied!
./package.json
1
{
2
"name": "example-js",
3
"version": "0.0.1",
4
"description": "Hello world",
5
"private": true,
6
"scripts": {
7
"test": "echo \"Error: no test specified\" && exit 1"
8
},
9
"author": "",
10
"license": "MPL-2.0",
11
"devDependencies": {
12
"webpack": "^4.42.1",
13
"webpack-cli": "^3.3.11"
14
},
15
"dependencies": {
16
"http-server": "^0.12.1"
17
}
18
}
Copied!
./package-lock.json (empty)
1
​
Copied!
The code of the app might look like this
./src/index.js
1
function component() {
2
const element = document.createElement('div');
3
element.innerHTML = "hello world"
4
return element;
5
}
6
​
7
document.body.appendChild(component());
Copied!
./src/index.html
1
<!doctype html>
2
<html>
3
​
4
<head>
5
<title>Getting Started</title>
6
</head>
7
​
8
<body>
9
<script src="./main.js"></script>
10
</body>
11
​
12
</html>
Copied!
The build then might become
./Earthfile
1
FROM node:13.10.1-alpine3.11
2
WORKDIR /js-example
3
​
4
build:
5
COPY package.json package-lock.json ./
6
COPY src src
7
RUN mkdir -p ./dist && cp ./src/index.html ./dist/
8
RUN npm install
9
RUN npx webpack
10
SAVE ARTIFACT dist /dist AS LOCAL ./dist
11
​
12
docker:
13
COPY package.json package-lock.json ./
14
RUN npm install
15
COPY +build/dist dist
16
EXPOSE 8080
17
ENTRYPOINT ["/js-example/node_modules/http-server/bin/http-server", "./dist"]
18
SAVE IMAGE js-example:latest
Copied!

Note

To copy the files for this example ( Part 3 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/js:main+part3/part3 ./part3
Copied!
./build.gradle
1
apply plugin: 'java'
2
apply plugin: 'application'
3
​
4
mainClassName = 'hello.HelloWorld'
5
​
6
repositories {
7
mavenCentral()
8
}
9
​
10
jar {
11
baseName = 'hello-world'
12
version = '0.0.1'
13
}
14
​
15
sourceCompatibility = 1.8
16
targetCompatibility = 1.8
17
​
18
dependencies {
19
compile "joda-time:joda-time:2.2"
20
testCompile "junit:junit:4.12"
21
}
Copied!
The code of the app might look like this
./src/main/java/hello/HelloWorld.java
1
package hello;
2
​
3
import org.joda.time.LocalTime;
4
​
5
public class HelloWorld {
6
public static void main(String[] args) {
7
LocalTime currentTime = new LocalTime();
8
System.out.println(currentTime + " - hello world");
9
}
10
}
Copied!
The Earthfile file would not change
./Earthfile
1
FROM openjdk:8-jdk-alpine
2
RUN apk add --update --no-cache gradle
3
WORKDIR /java-example
4
​
5
build:
6
COPY build.gradle ./
7
COPY src src
8
RUN gradle build
9
RUN gradle install
10
SAVE ARTIFACT build/install/java-example/bin /bin AS LOCAL build/bin
11
SAVE ARTIFACT build/install/java-example/lib /lib AS LOCAL build/lib
12
​
13
docker:
14
COPY +build/bin bin
15
COPY +build/lib lib
16
ENTRYPOINT ["/java-example/bin/java-example"]
17
SAVE IMAGE java-example:latest
Copied!

Note

To copy the files for this example ( Part 3 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/java:main+part3/part3 ./part3
Copied!
./requirements.txt
1
Markdown==3.2.2
Copied!
The code of the app would now look like this
./src/hello.py
1
from markdown import markdown
2
​
3
def hello():
4
return markdown("## hello world")
5
​
6
print(hello())
Copied!
The build might then become as follows.
./Earthfile
1
FROM python:3
2
WORKDIR /code
3
​
4
build:
5
RUN pip install wheel
6
COPY requirements.txt ./
7
RUN pip wheel -r requirements.txt --wheel-dir=wheels
8
COPY src src
9
SAVE ARTIFACT src /src
10
SAVE ARTIFACT wheels /wheels
11
​
12
docker:
13
COPY +build/src src
14
COPY +build/wheels wheels
15
COPY requirements.txt ./
16
RUN pip install --no-index --find-links=wheels -r requirements.txt
17
ENTRYPOINT ["python3", "./src/hello.py"]
18
SAVE IMAGE python-example:latest
Copied!

Note

To copy the files for this example ( Part 3 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/python:main+part3/part3 ./part3
Copied!
However, as we build this new setup and make changes to the main source code, we notice that the dependencies are downloaded every single time we change the source code. While the build is not necessarily incorrect, it is inefficient for proper development speed.
To improve the speed we will make some changes in part 4 of the tutorial.
Last modified 24d ago
Copy link