Skip to content

Commit 97025cb

Browse files
committed
Initial version of a Maven plugin to run functions.
When appropriately configured in pom.xml, you can use `mvn function:run` to compile your function and start a web server that serves it. The version of the plugin artifact will track the version of the Invoker (aka Functions Framework). Since the plugin depends on and runs the Invoker, any time we change the latter we need to change the former. Further documentation to follow. PiperOrigin-RevId: 294293170
1 parent f17c223 commit 97025cb

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.google.cloud.functions.invoker</groupId>
7+
<artifactId>java-function-invoker-parent</artifactId>
8+
<version>1.0.0-alpha-2-rc3-SNAPSHOT</version>
9+
</parent>
10+
11+
<groupId>com.google.cloud.functions</groupId>
12+
<artifactId>function-maven-plugin</artifactId>
13+
<packaging>maven-plugin</packaging>
14+
<version>1.0.0-alpha-2-rc3-SNAPSHOT</version>
15+
<name>Functions Framework Plugin</name>
16+
<description>A Maven plugin that allows functions to be run locally
17+
using the Java Functions Framework.</description>
18+
<url>http://maven.apache.org</url>
19+
20+
<properties>
21+
<maven.compiler.release>8</maven.compiler.release>
22+
<maven.compiler.source>8</maven.compiler.source>
23+
<maven.compiler.target>8</maven.compiler.target>
24+
</properties>
25+
26+
<dependencies>
27+
28+
<dependency>
29+
<groupId>org.apache.maven</groupId>
30+
<artifactId>maven-plugin-api</artifactId>
31+
<version>3.6.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.maven</groupId>
35+
<artifactId>maven-core</artifactId>
36+
<version>3.6.3</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.maven.plugin-tools</groupId>
40+
<artifactId>maven-plugin-annotations</artifactId>
41+
<version>3.6.0</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.google.cloud.functions.invoker</groupId>
47+
<artifactId>java-function-invoker</artifactId>
48+
<version>1.0.0-alpha-2-rc3-SNAPSHOT</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.13</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-plugin-plugin</artifactId>
64+
<version>3.6.0</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.google.cloud.functions.plugin;
2+
3+
import com.google.cloud.functions.invoker.runner.Invoker;
4+
import java.io.File;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import org.apache.maven.plugin.AbstractMojo;
9+
import org.apache.maven.plugin.MojoExecutionException;
10+
import org.apache.maven.plugins.annotations.Execute;
11+
import org.apache.maven.plugins.annotations.LifecyclePhase;
12+
import org.apache.maven.plugins.annotations.Mojo;
13+
import org.apache.maven.plugins.annotations.Parameter;
14+
import org.apache.maven.plugins.annotations.ResolutionScope;
15+
16+
@Mojo(name = "run",
17+
defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
18+
requiresDependencyResolution = ResolutionScope.COMPILE,
19+
requiresDependencyCollection = ResolutionScope.COMPILE)
20+
@Execute(phase = LifecyclePhase.COMPILE)
21+
public class RunFunction extends AbstractMojo {
22+
23+
/**
24+
* The name of the function to run. This is the name of a class that implements one of the
25+
* interfaces in {@code com.google.cloud.functions}.
26+
*/
27+
@Parameter(property = "run.functionTarget")
28+
private String functionTarget;
29+
30+
/**
31+
* The port on which the HTTP server wrapping the function should listen.
32+
*/
33+
@Parameter(property = "run.port", defaultValue = "8080")
34+
private Integer port;
35+
36+
/**
37+
* Used to determine what classpath needs to be used to load the function. This parameter is
38+
* injected by Maven and can't be set explicitly in a pom.xml file.
39+
*/
40+
@Parameter(defaultValue = "${project.compileClasspathElements}", readonly = true, required = true)
41+
private List<String> compilePath;
42+
43+
public void execute() throws MojoExecutionException {
44+
String classpath = String.join(File.pathSeparator, compilePath);
45+
List<String> args = new ArrayList<>();
46+
args.addAll(Arrays.asList("--classpath", classpath));
47+
if (functionTarget != null) {
48+
args.addAll(Arrays.asList("--target", functionTarget));
49+
}
50+
try {
51+
getLog().info("Calling Invoker with " + args);
52+
Invoker.main(args.toArray(new String[0]));
53+
} catch (Exception e) {
54+
getLog().error("Could not invoke function: " + e, e);
55+
throw new MojoExecutionException("Could not invoke function", e);
56+
}
57+
}
58+
}

invoker/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<modules>
2828
<module>core</module>
2929
<module>testfunction</module>
30+
<module>function-maven-plugin</module>
3031
</modules>
3132

3233
<properties>

invoker/testfunction/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<plugin>
8686
<groupId>org.apache.maven.plugins</groupId>
8787
<artifactId>maven-deploy-plugin</artifactId>
88+
<version>3.0.0-M1</version>
8889
<configuration>
8990
<skip>true</skip>
9091
</configuration>

0 commit comments

Comments
 (0)