Class AbstractProtocMojo
- java.lang.Object
-
- org.apache.maven.plugin.AbstractMojo
-
- org.xolstice.maven.plugin.protobuf.AbstractProtocMojo
-
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled
,org.apache.maven.plugin.Mojo
- Direct Known Subclasses:
AbstractProtocCompileMojo
,AbstractProtocTestCompileMojo
abstract class AbstractProtocMojo extends org.apache.maven.plugin.AbstractMojo
Abstract Mojo implementation.This class is extended by
ProtocCompileMojo
andProtocTestCompileMojo
in order to override the specific configuration for compiling the main or test classes respectively.
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.File[]
additionalProtoPathElements
Additional source paths for.proto
definitions.private org.apache.maven.artifact.factory.ArtifactFactory
artifactFactory
A factory for Maven artifact definitions.private org.apache.maven.artifact.resolver.ArtifactResolver
artifactResolver
A component that implements resolution of Maven artifacts from repositories.protected boolean
attachDescriptorSet
If set totrue
, the generated descriptor set will be attached to the build.protected boolean
attachProtoSources
If set totrue
, then the specified protobuf source files from this project will be attached as resources to the build, for subsequent inclusion into the final artifact.protected org.sonatype.plexus.build.incremental.BuildContext
buildContext
Build context that tracks changes to the source and target files.private boolean
checkStaleness
Normallyprotoc
is invoked on every execution of the plugin.private boolean
clearOutputDirectory
Whentrue
, the output directory will be cleared out prior to code generation.private static java.lang.String
DEFAULT_INCLUDES
protected java.lang.String
descriptorSetFileName
The descriptor set file name.private java.lang.String[]
excludes
A list of <exclude> elements specifying the protobuf definition files (by pattern) that should be excluded from compilation.private boolean
forceMojoExecution
Usually most of protobuf mojos will not get executed on parent poms (i.e.private boolean
hashDependentPaths
Set this tofalse
to disable hashing of dependent jar paths.private static char[]
HEX_CHARS
protected boolean
includeDependenciesInDescriptorSet
Iftrue
andwriteDescriptorSet
has been set, the compiler will include all dependencies in the descriptor set making it "self-contained".private java.lang.String[]
includes
A list of <include> elements specifying the protobuf definition files (by pattern) that should be included in compilation.protected boolean
includeSourceInfoInDescriptorSet
Iftrue
andwriteDescriptorSet
has been set, do not strip SourceCodeInfo from the FileDescriptorProto.private org.apache.maven.artifact.repository.ArtifactRepository
localRepository
This is the path to the local mavenrepository
.protected org.apache.maven.project.MavenProject
project
The current Maven project.protected org.apache.maven.project.MavenProjectHelper
projectHelper
A helper used to add resources to the project.private static java.lang.String
PROTO_FILE_SUFFIX
private java.lang.String
protocArtifact
Protobuf compiler artifact specification, ingroupId:artifactId:version[:type[:classifier]]
format.private java.lang.String
protocExecutable
This is the path to theprotoc
executable.private java.io.File
protocPluginDirectory
A directory where native launchers for java protoc plugins will be generated.private java.util.List<ProtocPlugin>
protocPlugins
Specifies one of more custom protoc plugins, written in Java and available as Maven artifacts.private java.util.List<org.apache.maven.artifact.repository.ArtifactRepository>
remoteRepositories
Remote repositories for artifact resolution.private org.apache.maven.repository.RepositorySystem
repositorySystem
A component that handles resolution of Maven artifacts.private org.apache.maven.artifact.resolver.ResolutionErrorHandler
resolutionErrorHandler
A component that handles resolution errors.protected org.apache.maven.execution.MavenSession
session
The current Maven Session Object.private boolean
skip
Whentrue
, skip the execution.private long
staleMillis
Sets the granularity in milliseconds of the last modification date for testing whether source protobuf definitions need recompilation.private java.io.File
tempDirectory
A directory where temporary files will be generated.private java.io.File
temporaryProtoFileDirectory
Sinceprotoc
cannot access jars, proto files in dependencies are extracted to this location and deleted on exit.protected org.apache.maven.toolchain.ToolchainManager
toolchainManager
An optional tool chain manager.protected boolean
useArgumentFile
If set totrue
, all command line arguments to protoc will be written to a file, and only a path to that file will be passed to protoc on the command line.protected boolean
writeDescriptorSet
If set totrue
, the compiler will generate a binary descriptor set file for the specified.proto
files.
-
Constructor Summary
Constructors Constructor Description AbstractProtocMojo()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addProtocBuilderParameters(Protoc.Builder protocBuilder)
Adds mojo-specific parameters to the protoc builder.protected boolean
checkFilesUpToDate(java.lang.Iterable<java.io.File> sourceFiles, java.lang.Iterable<java.io.File> targetFiles)
Checks that the source files don't have modification time that is later than the target files.protected void
checkParameters()
protected org.apache.maven.artifact.Artifact
createDependencyArtifact(java.lang.String artifactSpec)
Creates a dependency artifact from a specification ingroupId:artifactId:version[:type[:classifier]]
format.protected org.apache.maven.artifact.Artifact
createDependencyArtifact(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String type, java.lang.String classifier)
protected void
createProtocPlugins()
Generates native launchers for java protoc plugins.protected java.lang.String
detectJavaHome()
Attempts to detect java home directory, usingjdk
toolchain if available, with a fallback tojava.home
system property.protected void
doAttachFiles()
protected abstract void
doAttachGeneratedFiles()
protected abstract void
doAttachProtoSources()
void
execute()
Executes the mojo.protected static java.util.List<java.io.File>
findGeneratedFilesInDirectory(java.io.File directory)
protected java.util.List<java.io.File>
findProtoFilesInDirectories(java.lang.Iterable<java.io.File> directories)
protected java.util.List<java.io.File>
findProtoFilesInDirectory(java.io.File directory)
protected java.util.List<java.io.File>
getDependencyArtifactFiles()
Gets theFile
for each dependency artifact.protected abstract java.util.List<org.apache.maven.artifact.Artifact>
getDependencyArtifacts()
protected abstract java.io.File
getDescriptorSetOutputDirectory()
Returns output directory for descriptor set file.protected java.lang.String[]
getExcludes()
protected java.lang.String[]
getIncludes()
protected abstract java.io.File
getOutputDirectory()
Returns the output directory for generated sources.protected abstract java.io.File
getProtoSourceRoot()
protected boolean
hasDelta(java.lang.Iterable<java.io.File> files)
Checks if the injected build context has changes in any of the specified files.protected static long
lastModified(java.lang.Iterable<java.io.File> files)
Returns timestamp for the most recently modified file in the given set.protected java.util.List<java.io.File>
makeProtoPathFromJars(java.io.File temporaryProtoFileDirectory, java.lang.Iterable<java.io.File> classpathElementFiles)
Unpacks proto descriptors that are bundled inside dependent artifacts into a temporary directory.private static java.lang.String
md5Hash(java.lang.String string)
protected java.io.File
resolveBinaryArtifact(org.apache.maven.artifact.Artifact artifact)
protected boolean
skipMojo()
Determine if the mojo execution should get skipped.protected static java.lang.String
toHexString(byte[] byteArray)
protected java.lang.String
truncatePath(java.lang.String jarPath)
Truncates the path of jar files so that they are relative to the local repository.
-
-
-
Field Detail
-
PROTO_FILE_SUFFIX
private static final java.lang.String PROTO_FILE_SUFFIX
- See Also:
- Constant Field Values
-
DEFAULT_INCLUDES
private static final java.lang.String DEFAULT_INCLUDES
- See Also:
- Constant Field Values
-
project
@Parameter(defaultValue="${project}", readonly=true) protected org.apache.maven.project.MavenProject project
The current Maven project.
-
session
@Parameter(defaultValue="${session}", readonly=true) protected org.apache.maven.execution.MavenSession session
The current Maven Session Object.- Since:
- 0.2.0
-
buildContext
@Component protected org.sonatype.plexus.build.incremental.BuildContext buildContext
Build context that tracks changes to the source and target files.- Since:
- 0.3.0
-
toolchainManager
@Component protected org.apache.maven.toolchain.ToolchainManager toolchainManager
An optional tool chain manager.- Since:
- 0.2.0
-
projectHelper
@Component protected org.apache.maven.project.MavenProjectHelper projectHelper
A helper used to add resources to the project.
-
artifactFactory
@Component private org.apache.maven.artifact.factory.ArtifactFactory artifactFactory
A factory for Maven artifact definitions.- Since:
- 0.3.1
-
artifactResolver
@Component private org.apache.maven.artifact.resolver.ArtifactResolver artifactResolver
A component that implements resolution of Maven artifacts from repositories.- Since:
- 0.3.1
-
repositorySystem
@Component private org.apache.maven.repository.RepositorySystem repositorySystem
A component that handles resolution of Maven artifacts.- Since:
- 0.4.0
-
resolutionErrorHandler
@Component private org.apache.maven.artifact.resolver.ResolutionErrorHandler resolutionErrorHandler
A component that handles resolution errors.- Since:
- 0.4.0
-
localRepository
@Parameter(required=true, readonly=true, property="localRepository") private org.apache.maven.artifact.repository.ArtifactRepository localRepository
This is the path to the local mavenrepository
.
-
remoteRepositories
@Parameter(required=true, readonly=true, defaultValue="${project.remoteArtifactRepositories}") private java.util.List<org.apache.maven.artifact.repository.ArtifactRepository> remoteRepositories
Remote repositories for artifact resolution.- Since:
- 0.3.0
-
tempDirectory
@Parameter(required=true, readonly=true, defaultValue="${project.build.directory}") private java.io.File tempDirectory
A directory where temporary files will be generated.- Since:
- 0.6.0
-
protocPluginDirectory
@Parameter(required=false, defaultValue="${project.build.directory}/protoc-plugins") private java.io.File protocPluginDirectory
A directory where native launchers for java protoc plugins will be generated.- Since:
- 0.3.0
-
protocExecutable
@Parameter(required=false, property="protocExecutable") private java.lang.String protocExecutable
This is the path to theprotoc
executable. When this parameter is not set, the plugin attempts to load aprotobuf
toolchain and use it locateprotoc
executable. If noprotobuf
toolchain is defined in the project, theprotoc
executable in thePATH
is used.
-
protocArtifact
@Parameter(required=false, property="protocArtifact") private java.lang.String protocArtifact
Protobuf compiler artifact specification, ingroupId:artifactId:version[:type[:classifier]]
format. When this parameter is set, the plugin attempts to resolve the specified artifact asprotoc
executable.- Since:
- 0.4.1
-
additionalProtoPathElements
@Parameter(required=false) private java.io.File[] additionalProtoPathElements
Additional source paths for.proto
definitions.
-
temporaryProtoFileDirectory
@Parameter(required=true, defaultValue="${project.build.directory}/protoc-dependencies") private java.io.File temporaryProtoFileDirectory
Sinceprotoc
cannot access jars, proto files in dependencies are extracted to this location and deleted on exit. This directory is always cleaned during execution.
-
hashDependentPaths
@Parameter(required=true, defaultValue="true") private boolean hashDependentPaths
Set this tofalse
to disable hashing of dependent jar paths. This plugin expands jars on the classpath looking for embedded.proto
files. Normally these paths are hashed (MD5) to avoid issues with long file names on windows. However if this property is set tofalse
longer paths will be used.
-
includes
@Parameter(required=false) private java.lang.String[] includes
A list of <include> elements specifying the protobuf definition files (by pattern) that should be included in compilation. When not specified, the default includes will be:
<includes>
<include>**/*.proto</include>
</includes>
-
excludes
@Parameter(required=false) private java.lang.String[] excludes
A list of <exclude> elements specifying the protobuf definition files (by pattern) that should be excluded from compilation. When not specified, the default excludes will be empty:
<excludes>
</excludes>
-
attachProtoSources
@Parameter(required=true, defaultValue="true") protected boolean attachProtoSources
If set totrue
, then the specified protobuf source files from this project will be attached as resources to the build, for subsequent inclusion into the final artifact. This is the default behaviour, as it allows downstream projects to import protobuf definitions from the upstream projects, and those imports are automatically resolved at build time.If distribution of
.proto
source files is undesirable for security reasons or because of other considerations, then this parameter should be set tofalse
.- Since:
- 0.4.1
-
descriptorSetFileName
@Parameter(required=true, defaultValue="${project.build.finalName}.protobin") protected java.lang.String descriptorSetFileName
The descriptor set file name. Only used ifwriteDescriptorSet
is set totrue
.- Since:
- 0.3.0
-
writeDescriptorSet
@Parameter(required=true, defaultValue="false") protected boolean writeDescriptorSet
If set totrue
, the compiler will generate a binary descriptor set file for the specified.proto
files.- Since:
- 0.3.0
-
attachDescriptorSet
@Parameter(required=true, defaultValue="false") protected boolean attachDescriptorSet
If set totrue
, the generated descriptor set will be attached to the build.- Since:
- 0.4.1
-
includeDependenciesInDescriptorSet
@Parameter(required=false, defaultValue="false") protected boolean includeDependenciesInDescriptorSet
Iftrue
andwriteDescriptorSet
has been set, the compiler will include all dependencies in the descriptor set making it "self-contained".- Since:
- 0.3.0
-
includeSourceInfoInDescriptorSet
@Parameter(required=false, defaultValue="false") protected boolean includeSourceInfoInDescriptorSet
Iftrue
andwriteDescriptorSet
has been set, do not strip SourceCodeInfo from the FileDescriptorProto. This results in vastly larger descriptors that include information about the original location of each decl in the source file as well as surrounding comments.- Since:
- 0.4.4
-
useArgumentFile
@Parameter(required=false, defaultValue="false") protected boolean useArgumentFile
If set totrue
, all command line arguments to protoc will be written to a file, and only a path to that file will be passed to protoc on the command line. This helps prevent Command line is too long errors when the number of.proto
files is large.NOTE: This is only supported for protoc 3.5.0 and higher.
- Since:
- 0.6.0
-
protocPlugins
@Parameter(required=false) private java.util.List<ProtocPlugin> protocPlugins
Specifies one of more custom protoc plugins, written in Java and available as Maven artifacts. An executable plugin will be created at execution time. On UNIX the executable is a shell script and on Windows it is a WinRun4J .exe and .ini.
-
staleMillis
@Parameter(required=false, defaultValue="0") private long staleMillis
Sets the granularity in milliseconds of the last modification date for testing whether source protobuf definitions need recompilation.This parameter is only used when
checkStaleness
parameter is set totrue
.If the project is built on NFS it's recommended to set this parameter to
10000
.
-
checkStaleness
@Parameter(required=false, defaultValue="false") private boolean checkStaleness
Normallyprotoc
is invoked on every execution of the plugin. Setting this parameter totrue
will enable checking timestamps of source protobuf definitions vs. generated sources.- See Also:
staleMillis
-
skip
@Parameter(required=false, property="protoc.skip", defaultValue="false") private boolean skip
Whentrue
, skip the execution.- Since:
- 0.2.0
-
forceMojoExecution
@Parameter(required=false, property="protoc.force", defaultValue="false") private boolean forceMojoExecution
Usually most of protobuf mojos will not get executed on parent poms (i.e. projects with packaging type 'pom'). Setting this parameter totrue
will force the execution of this mojo, even if it would usually get skipped in this case.- Since:
- 0.2.0
-
clearOutputDirectory
@Parameter(required=false, defaultValue="true") private boolean clearOutputDirectory
Whentrue
, the output directory will be cleared out prior to code generation. With the latest versions of protoc (2.5.0 or later) this is generally not required, although some earlier versions reportedly had issues with running two code generations in a row without clearing out the output directory in between.- Since:
- 0.4.0
-
HEX_CHARS
private static final char[] HEX_CHARS
-
-
Method Detail
-
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionException, org.apache.maven.plugin.MojoFailureException
Executes the mojo.- Throws:
org.apache.maven.plugin.MojoExecutionException
org.apache.maven.plugin.MojoFailureException
-
createProtocPlugins
protected void createProtocPlugins()
Generates native launchers for java protoc plugins. These launchers will later be added as parameters for protoc compiler.- Since:
- 0.3.0
-
detectJavaHome
protected java.lang.String detectJavaHome()
Attempts to detect java home directory, usingjdk
toolchain if available, with a fallback tojava.home
system property.- Returns:
- path to java home directory.
- Since:
- 0.3.0
-
addProtocBuilderParameters
protected void addProtocBuilderParameters(Protoc.Builder protocBuilder)
Adds mojo-specific parameters to the protoc builder.- Parameters:
protocBuilder
- the builder to be modified.
-
skipMojo
protected boolean skipMojo()
Determine if the mojo execution should get skipped.
This is the case if:skip
istrue
- if the mojo gets executed on a project with packaging type 'pom' and
forceMojoExecution
isfalse
- Returns:
true
if the mojo execution should be skipped.- Since:
- 0.2.0
-
findGeneratedFilesInDirectory
protected static java.util.List<java.io.File> findGeneratedFilesInDirectory(java.io.File directory)
-
lastModified
protected static long lastModified(java.lang.Iterable<java.io.File> files)
Returns timestamp for the most recently modified file in the given set.- Parameters:
files
- a collection of file descriptors.- Returns:
- timestamp of the most recently modified file.
-
checkFilesUpToDate
protected boolean checkFilesUpToDate(java.lang.Iterable<java.io.File> sourceFiles, java.lang.Iterable<java.io.File> targetFiles)
Checks that the source files don't have modification time that is later than the target files.- Parameters:
sourceFiles
- a collection of source files.targetFiles
- a collection of target files.- Returns:
true
, if source files are not later than the target files;false
, otherwise.
-
hasDelta
protected boolean hasDelta(java.lang.Iterable<java.io.File> files)
Checks if the injected build context has changes in any of the specified files.- Parameters:
files
- files to be checked for changes.- Returns:
true
, if at least one file has changes;false
, if no files have changes.- Since:
- 0.3.0
-
checkParameters
protected void checkParameters()
-
getProtoSourceRoot
protected abstract java.io.File getProtoSourceRoot()
-
getIncludes
protected java.lang.String[] getIncludes()
-
getExcludes
protected java.lang.String[] getExcludes()
-
getDependencyArtifacts
protected abstract java.util.List<org.apache.maven.artifact.Artifact> getDependencyArtifacts()
-
getOutputDirectory
protected abstract java.io.File getOutputDirectory()
Returns the output directory for generated sources. Depends on build phase so must be defined in concrete implementation.- Returns:
- output directory for generated sources.
-
getDescriptorSetOutputDirectory
protected abstract java.io.File getDescriptorSetOutputDirectory()
Returns output directory for descriptor set file. Depends on build phase so must be defined in concrete implementation.- Returns:
- output directory for generated descriptor set.
- Since:
- 0.3.0
-
doAttachFiles
protected void doAttachFiles()
-
doAttachProtoSources
protected abstract void doAttachProtoSources()
-
doAttachGeneratedFiles
protected abstract void doAttachGeneratedFiles()
-
getDependencyArtifactFiles
protected java.util.List<java.io.File> getDependencyArtifactFiles()
Gets theFile
for each dependency artifact.- Returns:
- A list of all dependency artifacts.
-
makeProtoPathFromJars
protected java.util.List<java.io.File> makeProtoPathFromJars(java.io.File temporaryProtoFileDirectory, java.lang.Iterable<java.io.File> classpathElementFiles)
Unpacks proto descriptors that are bundled inside dependent artifacts into a temporary directory. This is needed because protobuf compiler cannot handle imported descriptors that are packed inside jar files.- Parameters:
temporaryProtoFileDirectory
- temporary directory to serve as root for unpacked structure.classpathElementFiles
- classpath elements, can be either jar files or directories.- Returns:
- a list of import roots for protobuf compiler (these will all be subdirectories of the temporary directory).
-
findProtoFilesInDirectory
protected java.util.List<java.io.File> findProtoFilesInDirectory(java.io.File directory)
-
findProtoFilesInDirectories
protected java.util.List<java.io.File> findProtoFilesInDirectories(java.lang.Iterable<java.io.File> directories)
-
truncatePath
protected java.lang.String truncatePath(java.lang.String jarPath)
Truncates the path of jar files so that they are relative to the local repository.- Parameters:
jarPath
- the full path of a jar file.- Returns:
- the truncated path relative to the local repository or root of the drive.
-
md5Hash
private static java.lang.String md5Hash(java.lang.String string)
-
toHexString
protected static java.lang.String toHexString(byte[] byteArray)
-
resolveBinaryArtifact
protected java.io.File resolveBinaryArtifact(org.apache.maven.artifact.Artifact artifact)
-
createDependencyArtifact
protected org.apache.maven.artifact.Artifact createDependencyArtifact(java.lang.String artifactSpec)
Creates a dependency artifact from a specification ingroupId:artifactId:version[:type[:classifier]]
format.- Parameters:
artifactSpec
- artifact specification.- Returns:
- artifact object instance.
-
createDependencyArtifact
protected org.apache.maven.artifact.Artifact createDependencyArtifact(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String type, java.lang.String classifier)
-
-