Skip to content

Commit 3b91d93

Browse files
authored
Merge pull request #763 from ascopes/feature/gh-762-additional-args
GH-762: Allow passing additional flags to protoc
2 parents 8b30b08 + 64113b7 commit 3b91d93

14 files changed

Lines changed: 325 additions & 3 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<groupId>io.github.ascopes</groupId>
2323
<artifactId>protobuf-maven-plugin-parent</artifactId>
24-
<version>3.7.1-SNAPSHOT</version>
24+
<version>3.8.0-SNAPSHOT</version>
2525

2626
<name>Protobuf Maven Plugin Parent</name>
2727
<description>Parent POM for the Protobuf Maven Plugin.</description>

protobuf-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>io.github.ascopes</groupId>
2424
<artifactId>protobuf-maven-plugin-parent</artifactId>
25-
<version>3.7.1-SNAPSHOT</version>
25+
<version>3.8.0-SNAPSHOT</version>
2626
</parent>
2727

2828
<artifactId>protobuf-maven-plugin</artifactId>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright (C) 2023 - 2025, Ashley Scopes.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
invoker.goals = clean package
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) 2023 - 2025, Ashley Scopes.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>@project.groupId@.it</groupId>
26+
<artifactId>integration-test-parent</artifactId>
27+
<version>@project.version@</version>
28+
<relativePath>../setup/pom.xml</relativePath>
29+
</parent>
30+
31+
<groupId>gh-762-additional-args</groupId>
32+
<artifactId>gh-762-additional-args</artifactId>
33+
34+
<properties>
35+
<!-- Frozen to a very old version that still kept optional as experimental.
36+
This allows testing the enabling flag. -->
37+
<protobuf.version>3.12.4</protobuf.version>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.protobuf</groupId>
43+
<artifactId>protobuf-java</artifactId>
44+
<scope>compile</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.junit.jupiter</groupId>
49+
<artifactId>junit-jupiter</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
</plugin>
60+
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
</plugin>
65+
66+
<plugin>
67+
<groupId>@project.groupId@</groupId>
68+
<artifactId>@project.artifactId@</artifactId>
69+
70+
<configuration>
71+
<arguments>
72+
<argument>--experimental_allow_proto3_optional</argument>
73+
</arguments>
74+
</configuration>
75+
76+
<executions>
77+
<execution>
78+
<goals>
79+
<goal>generate</goal>
80+
</goals>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2023 - 2025, Ashley Scopes.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// Only execute on x86 systems, aarch64 ports do not exist for all platforms
17+
// we use in our CI pipeline for the old version of protoc we use for this
18+
// test.
19+
if (!System.getProperty("os.arch").equalsIgnoreCase("amd64")) {
20+
println("Skipping this test case as the system is not x86_64")
21+
return false
22+
} else {
23+
return true
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (C) 2023 - 2025, Ashley Scopes.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
syntax = "proto3";
18+
19+
option java_multiple_files = true;
20+
option java_package = "org.example.helloworld";
21+
22+
package org.example.helloworld;
23+
24+
message GreetingRequest {
25+
string name = 1;
26+
optional string remark = 2;
27+
}
28+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2023 - 2025, Ashley Scopes.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.example.helloworld;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.util.ArrayList;
24+
import org.junit.jupiter.api.Test;
25+
26+
class ProtobufTest {
27+
@Test
28+
void generatedProtobufSourcesAreFullMessages() throws Throwable {
29+
// When
30+
var superClasses = new ArrayList<String>();
31+
Class<?> superClass = GreetingRequest.class;
32+
33+
do {
34+
superClasses.add(superClass.getName());
35+
superClass = superClass.getSuperclass();
36+
} while (superClass != null);
37+
38+
// Then
39+
// GeneratedMessageV3 for protobuf-java 3.25.3 and older.
40+
// GeneratedMessage for protobuf-java 4.26.0 and newer.
41+
assertTrue(superClasses.contains("com.google.protobuf.GeneratedMessageV3"));
42+
}
43+
44+
@Test
45+
void generatedProtobufSourcesAreValid() throws Throwable {
46+
// Given
47+
var expectedGreetingRequest = GreetingRequest
48+
.newBuilder()
49+
.setName("Ashley")
50+
.build();
51+
52+
// When
53+
var baos = new ByteArrayOutputStream();
54+
expectedGreetingRequest.writeTo(baos);
55+
var actualGreetingRequest = GreetingRequest.parseFrom(baos.toByteArray());
56+
57+
assertNotEquals(0, baos.toByteArray().length);
58+
59+
// Then
60+
assertEquals(expectedGreetingRequest.getName(), actualGreetingRequest.getName());
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2023 - 2025, Ashley Scopes.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import java.nio.file.Path
17+
18+
import static org.assertj.core.api.Assertions.assertThat
19+
20+
Path baseDirectory = basedir.toPath().toAbsolutePath()
21+
Path generatedSourcesDir = baseDirectory.resolve("target/generated-sources/protobuf")
22+
Path classesDir = baseDirectory.resolve("target/classes")
23+
List<String> expectedGeneratedFiles = [
24+
"org/example/helloworld/Helloworld",
25+
"org/example/helloworld/GreetingRequest",
26+
"org/example/helloworld/GreetingRequestOrBuilder",
27+
]
28+
29+
assertThat(generatedSourcesDir).isDirectory()
30+
31+
assertThat(classesDir).isDirectory()
32+
33+
expectedGeneratedFiles.forEach {
34+
assertThat(generatedSourcesDir.resolve("${it}.java"))
35+
.exists()
36+
.isNotEmptyFile()
37+
assertThat(classesDir.resolve("${it}.class"))
38+
.exists()
39+
.isNotEmptyFile()
40+
}
41+
42+
return true

protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/generation/GenerationRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
@Immutable
3838
public interface GenerationRequest {
3939

40+
/**
41+
* Additional arguments to pass to {@code protoc}.
42+
*
43+
* @return the arguments list.
44+
*/
45+
List<String> getArguments();
46+
4047
/**
4148
* Binary {@code protoc} plugins that should be resolved from Maven
4249
* repositories.

protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/generation/ProtobufBuildOrchestrator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ private ProtocInvocation createProtocInvocation(
386386
.collect(Collectors.toUnmodifiableList());
387387

388388
return ImmutableProtocInvocation.builder()
389+
.arguments(request.getArguments())
389390
.descriptorSourceFiles(filesToCompile.getDescriptorFiles())
390391
.environmentVariables(request.getEnvironmentVariables())
391392
.fatalWarnings(request.isFatalWarnings())

0 commit comments

Comments
 (0)