Skip to content

Commit 4aed569

Browse files
authored
Bazel example with mapstruct (#109)
- added an example for build bazel project with mapstruct - added README about how to build/test this example - use same code from other example - updated .gitignore to exclude bazel symbol linked folders - added bazel github action
1 parent 7a69dc6 commit 4aed569

File tree

10 files changed

+344
-0
lines changed

10 files changed

+344
-0
lines changed

.github/workflows/main.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,38 @@ jobs:
6262
java-version: 8
6363
- name: 'Test'
6464
run: ./mvnw ${MAVEN_ARGS} -U clean verify
65+
linux_bazel:
66+
strategy:
67+
fail-fast: false
68+
matrix:
69+
java: [8, 11]
70+
name: 'Linux JDK ${{ matrix.java }} Bazel'
71+
runs-on: ubuntu-latest
72+
steps:
73+
- name: Checkout
74+
uses: actions/checkout@v2
75+
- name: Set up JDK
76+
uses: actions/setup-java@v1
77+
with:
78+
java-version: ${{ matrix.java }}
79+
- name: Mount bazel cache
80+
uses: actions/cache@v1
81+
with:
82+
path: "/home/runner/.cache/bazel"
83+
key: bazel
84+
- name: Install bazel
85+
run: |
86+
curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.1.0/bazelisk-linux-amd64"
87+
mkdir -p "${GITHUB_WORKSPACE}/bin/"
88+
mv bazelisk-linux-amd64 "${GITHUB_WORKSPACE}/bin/bazel"
89+
chmod +x "${GITHUB_WORKSPACE}/bin/bazel"
90+
- name: Test
91+
run: |
92+
"${GITHUB_WORKSPACE}/bin/bazel" test //...
93+
- name: Build
94+
run: |
95+
"${GITHUB_WORKSPACE}/bin/bazel" build //...
96+
97+
98+
99+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ test-output
2222

2323
# Gradle
2424
.gradle
25+
26+
# Bazel
27+
**/bazel-*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Currently, the following examples exist:
66

77
* _mapstruct-on-ant_: Shows how to use MapStruct in Ant-based projects; to build this example, run `ant build` on the command line
88
* _mapstruct-on-gradle_: Shows how to use MapStruct in Gradle-based projects; to build the example project, run `./gradlew clean build` on the command line
9+
* _mapstruct-on-bazel_: Shows how to use MapStruct in Bazel-based projects; to build the example project, run `bazel build //...` on the command line, to test the project, run `bazel test //...`.
910
* _mapstruct-lombok_: Shows how to use MapStruct together with Lombok (with both a Maven `pom.xml` and a Gradle `build.gradle`); to build the example project, run either `mvn clean install` or `./gradlew clean build` on the command line
1011
* _mapstruct-iterable-non-iterable_: Shows how by means of a mapper util class conversions can be made from an iterable- to its non-iterable element
1112
* _mapstruct-mapping-from-map_: Shows how by means of a mapper util class and qualifiers extracting value can be carried out on Maps. Watch [mapstruct/mapstruct#1075](https://github.com/mapstruct/mapstruct/issues/1075) for native support.

WORKSPACE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
RULES_JVM_EXTERNAL_TAG = "3.3"
4+
5+
RULES_JVM_EXTERNAL_SHA = "d85951a92c0908c80bd8551002d66cb23c3434409c814179c0ff026b53544dab"
6+
7+
http_archive(
8+
name = "rules_jvm_external",
9+
sha256 = RULES_JVM_EXTERNAL_SHA,
10+
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
11+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
12+
)
13+
14+
load("@rules_jvm_external//:defs.bzl", "maven_install")
15+
16+
maven_install(
17+
artifacts = [
18+
"junit:junit:4.13.1",
19+
"org.easytesting:fest-assert:1.4",
20+
"org.mapstruct:mapstruct:1.3.1.Final",
21+
"org.mapstruct:mapstruct-processor:1.3.1.Final",
22+
],
23+
repositories = [
24+
"https://jcenter.bintray.com/",
25+
"https://maven.google.com",
26+
"https://repo1.maven.org/maven2",
27+
],
28+
)

mapstruct-on-bazel/BUILD.bazel

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
load("@rules_java//java:defs.bzl", "java_library", "java_plugin", "java_test")
2+
3+
java_library(
4+
name = "mapstruct-on-bazel",
5+
srcs = glob(["src/main/java/**/*.java"]),
6+
resources = glob(["src/main/resources/**"]),
7+
deps = [
8+
":mapstruct",
9+
],
10+
)
11+
12+
java_library(
13+
name = "tests",
14+
srcs = glob(["src/test/java/**/*.java"]),
15+
resources = glob(["src/test/resources/**"]),
16+
deps = [
17+
":mapstruct-on-bazel",
18+
"@maven//:junit_junit",
19+
"@maven//:org_easytesting_fest_assert",
20+
],
21+
)
22+
23+
java_test(
24+
name = "ConversionTest",
25+
test_class = "org.mapstruct.example.ConversionTest",
26+
runtime_deps = [
27+
":tests",
28+
],
29+
)
30+
31+
java_library(
32+
name = "mapstruct",
33+
exported_plugins = [
34+
":mapstruct_plugin",
35+
],
36+
neverlink = True,
37+
visibility = ["//visibility:public"],
38+
exports = [
39+
"@maven//:org_mapstruct_mapstruct",
40+
"@maven//:org_mapstruct_mapstruct_processor",
41+
],
42+
runtime_deps = [
43+
# add to runtime, as we need
44+
"@maven//:org_mapstruct_mapstruct",
45+
],
46+
)
47+
48+
java_plugin(
49+
name = "mapstruct_plugin",
50+
generates_api = True,
51+
processor_class = "org.mapstruct.ap.MappingProcessor",
52+
deps = [
53+
"@maven//:org_mapstruct_mapstruct_processor",
54+
],
55+
)

mapstruct-on-bazel/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Using MapStruct with Bazel
2+
3+
This example project shows how to use MapStruct with Bazel.
4+
The sample code is using the one from `mapstruct-on-ant`.
5+
6+
To build the project, run `bazel build //...` or `bazel build //mapstruct-on-bazel:all`
7+
8+
To test the project, run `bazel test //...` or `bazel test //mapstruct-on-bazel:ConversionTest`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.example;
7+
8+
public class Source {
9+
10+
private int foo;
11+
private Long bar;
12+
private int qax;
13+
private Long baz;
14+
private int zip;
15+
16+
public int getFoo() {
17+
return foo;
18+
}
19+
20+
public void setFoo(int foo) {
21+
this.foo = foo;
22+
}
23+
24+
public Long getBar() {
25+
return bar;
26+
}
27+
28+
public void setBar(Long bar) {
29+
this.bar = bar;
30+
}
31+
32+
public int getQax() {
33+
return qax;
34+
}
35+
36+
public void setQax(int qax) {
37+
this.qax = qax;
38+
}
39+
40+
public Long getBaz() {
41+
return baz;
42+
}
43+
44+
public void setBaz(Long baz) {
45+
this.baz = baz;
46+
}
47+
48+
public int getZip() {
49+
return zip;
50+
}
51+
52+
public void setZip(int zip) {
53+
this.zip = zip;
54+
}
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.example;
7+
8+
import org.mapstruct.InheritInverseConfiguration;
9+
import org.mapstruct.Mapper;
10+
import org.mapstruct.Mapping;
11+
import org.mapstruct.factory.Mappers;
12+
13+
@Mapper
14+
public interface SourceTargetMapper {
15+
16+
SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);
17+
18+
@Mapping(source = "qax", target = "baz")
19+
@Mapping(source = "baz", target = "qax")
20+
Target sourceToTarget(Source source);
21+
22+
@InheritInverseConfiguration
23+
Source targetToSource(Target target);
24+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.example;
7+
8+
public class Target {
9+
10+
private Long foo;
11+
private int bar;
12+
private Long baz;
13+
private int qax;
14+
private String zip;
15+
16+
public Long getFoo() {
17+
return foo;
18+
}
19+
20+
public void setFoo(Long foo) {
21+
this.foo = foo;
22+
}
23+
24+
public int getBar() {
25+
return bar;
26+
}
27+
28+
public void setBar(int bar) {
29+
this.bar = bar;
30+
}
31+
32+
public Long getBaz() {
33+
return baz;
34+
}
35+
36+
public void setBaz(Long baz) {
37+
this.baz = baz;
38+
}
39+
40+
public int getQax() {
41+
return qax;
42+
}
43+
44+
public void setQax(int qax) {
45+
this.qax = qax;
46+
}
47+
48+
public String getZip() {
49+
return zip;
50+
}
51+
52+
public void setZip(String zip) {
53+
this.zip = zip;
54+
}
55+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.example;
7+
8+
import org.junit.Test;
9+
10+
import static org.fest.assertions.Assertions.assertThat;
11+
12+
public class ConversionTest {
13+
14+
@Test
15+
public void shouldApplyConversions() {
16+
Source source = new Source();
17+
source.setFoo(42);
18+
source.setBar(23L);
19+
source.setZip(73);
20+
21+
Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source);
22+
23+
assertThat(target).isNotNull();
24+
assertThat(target.getFoo()).isEqualTo(42L);
25+
assertThat(target.getBar()).isEqualTo(23);
26+
assertThat(target.getZip()).isEqualTo("73");
27+
}
28+
29+
@Test
30+
public void shouldHandleNulls() {
31+
Source source = new Source();
32+
Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source);
33+
34+
assertThat(target).isNotNull();
35+
assertThat(target.getFoo()).isEqualTo(0L);
36+
assertThat(target.getBar()).isEqualTo(0);
37+
assertThat(target.getZip()).isEqualTo("0");
38+
}
39+
40+
@Test
41+
public void shouldApplyConversionsToMappedProperties() {
42+
Source source = new Source();
43+
source.setQax(42);
44+
source.setBaz(23L);
45+
46+
Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source);
47+
48+
assertThat(target).isNotNull();
49+
assertThat(target.getBaz()).isEqualTo(42L);
50+
assertThat(target.getQax()).isEqualTo(23);
51+
}
52+
53+
@Test
54+
public void shouldApplyConversionsForReverseMapping() {
55+
Target target = new Target();
56+
target.setFoo(42L);
57+
target.setBar(23);
58+
target.setZip("73");
59+
60+
Source source = SourceTargetMapper.INSTANCE.targetToSource(target);
61+
62+
assertThat(source).isNotNull();
63+
assertThat(source.getFoo()).isEqualTo(42);
64+
assertThat(source.getBar()).isEqualTo(23);
65+
assertThat(source.getZip()).isEqualTo(73);
66+
}
67+
68+
@Test
69+
public void shouldApplyConversionsToMappedPropertiesForReverseMapping() {
70+
Target target = new Target();
71+
target.setQax(42);
72+
target.setBaz(23L);
73+
74+
Source source = SourceTargetMapper.INSTANCE.targetToSource(target);
75+
76+
assertThat(source).isNotNull();
77+
assertThat(source.getBaz()).isEqualTo(42);
78+
assertThat(source.getQax()).isEqualTo(23);
79+
}
80+
}

0 commit comments

Comments
 (0)