Service Builder Gradle Plugin

The Service Builder Gradle plugin lets you generate a service layer defined in a Service Builder service.xml file.

The plugin has been successfully tested with Gradle 4.10.2.

Usage

To use the plugin, include it in your build script:

buildscript {
    dependencies {
        classpath group: "com.liferay", name: "com.liferay.gradle.plugins.service.builder", version: "2.2.46"
    }

    repositories {
        maven {
            url "https://repository-cdn.liferay.com/nexus/content/groups/public"
        }
    }
}

apply plugin: "com.liferay.portal.tools.service.builder"

The Service Builder plugin automatically applies the java plugin.

Since the plugin automatically resolves the Liferay Service Builder library as a dependency, you have to configure a repository that hosts the library and its transitive dependencies. The Liferay CDN repository hosts them all:

repositories {
    maven {
        url "https://repository-cdn.liferay.com/nexus/content/groups/public"
    }
}

Tasks

The plugin adds one task to your project:

NameDepends OnTypeDescription
buildService-BuildServiceTaskRuns the Liferay Service Builder.

The buildService task is automatically configured with sensible defaults, depending on whether the war plugin is applied, or whether the osgiModule property is true:

Property NameDefault Value
apiDir

If the war plugin is applied: ${project.webAppDir}/WEB-INF/service

Otherwise: null

hbmFile

If osgiModule is true: ${buildService.resourcesDir}/META-INF/module-hbm.xml

Otherwise: ${buildService.resourcesDir}/META-INF/portlet-hbm.xml

implDirThe first java directory of the main source set (by default: src/main/java).
inputFile

If the war plugin is applied: ${project.webAppDir}/WEB-INF/service.xml

Otherwise: ${project.projectDir}/service.xml

modelHintsFileThe file META-INF/portlet-model-hints.xml in the first resources directory of the main source set (by default: src/main/resources/META-INF/portlet-model-hints.xml).
pluginName

If osgiModule is true: ""

Otherwise: project.name

propsUtil

If osgiModule is true: "${bundleSymbolicName}.util.ServiceProps"
The bundleSymbolicName of the project is inferred via the OsgiHelper class.

Otherwise: "com.liferay.util.service.ServiceProps"

resourcesDirThe first resources directory of the main source set (by default: src/main/resources).
springFile

If osgiModule is true: the file META-INF/spring/module-spring.xml in the first resources directory of the main source set (by default: src/main/resources/META-INF/spring/module-spring.xml)

Otherwise: the file META-INF/portlet-spring.xml in the first resources directory of the main source set (by default: src/main/resources/META-INF/portlet-spring.xml)

sqlDir

If the war plugin is applied: ${project.webAppDir}/WEB-INF/sql

Otherwise: The directory META-INF/sql in the first resources directory of the main source set (by default: src/main/resources/META-INF/sql).

In the typical scenario of a data-driven Liferay OSGi application split in myapp-app, myapp-service and myapp-web modules, the service.xml file is usually contained in the root directory of myapp-service. In the build.gradle of the same module, it is enough to apply the com.liferay.service.builder plugin as described, and then add the following snippet to enable the use of Liferay Service Builder:

buildService {
    apiDir = "../myapp-api/src/main/java"
    testDir = "../myapp-test/src/testIntegration/java"
}

While apiDir is required, the testDir property assignment can be left out, in which case Arquillian-based integration test classes are generated.

BuildServiceTask

Tasks of type BuildWSDDTask extend JavaExec, so all its properties and methods, such as args and maxHeapSize are available. They also have the following properties set by default:

Property NameDefault Value
argsService Builder command line arguments
classpathproject.configurations.serviceBuilder
main"com.liferay.portal.tools.service.builder.ServiceBuilder"
systemProperties["file.encoding": "UTF-8"]

Task Properties

Property NameTypeDefault ValueDescription
apiDirFilenullA directory where the service API Java source files are generated. It sets the service.api.dir argument.
autoImportDefaultReferencesbooleantrueWhether to automatically add default references, like com.liferay.portal.ClassName, com.liferay.portal.Resource and com.liferay.portal.User, to the services. It sets the service.auto.import.default.references argument.
autoNamespaceTablesbooleantrueWhether to prefix table names by the namespace specified in the service.xml file. It sets the service.auto.namespace.tables argument.
beanLocatorUtilString"com.liferay.util.bean.PortletBeanLocatorUtil"The fully qualified class name of a bean locator class to use in the generated service classes. It sets the service.bean.locator.util argument.
buildNumberlong1A specific value to assign the build.number property in the service.properties file. It sets the service.build.number argument.
buildNumberIncrementbooleantrueWhether to automatically increment the build.number property in the service.properties file by one at every service generation. It sets the service.build.number.increment argument.
databaseNameMaxLengthint30The upper bound for database table and column name lengths to ensure it works on all databases. It sets the service.database.name.max.length argument.
hbmFileFilenullA Hibernate Mapping file to generate. It sets the service.hbm.file argument.
implDirFilenullA directory where the service Java source files are generated. It sets the service.impl.dir argument.
inputFileFilenullThe project’s service.xml file. It sets the service.input.file argument.
modelHintsConfigsSet["classpath*:META-INF/portal-model-hints.xml", "META-INF/portal-model-hints.xml", "classpath*:META-INF/ext-model-hints.xml", "classpath*:META-INF/portlet-model-hints.xml"]Paths to the model hints files for Liferay Service Builder to use in generating the service layer. It sets the service.model.hints.configs argument.
modelHintsFileFilenullA model hints file for the project. It sets the service.model.hints.file argument.
osgiModulebooleanfalseWhether to generate the service layer for OSGi modules. It sets the service.osgi.module argument.
pluginNameStringnullIf specified, a plugin can enable additional generation features, such as Clp class generation, for non-OSGi modules. It sets the service.plugin.name argument.
propsUtilStringnullThe fully qualified class name of the service properties util class to generate. It sets the service.props.util argument.
readOnlyPrefixesSet["fetch", "get", "has", "is", "load", "reindex", "search"]Prefixes of methods to consider read-only. It sets the service.read.only.prefixes argument.
resourceActionsConfigsSet["META-INF/resource-actions/default.xml", "resource-actions/default.xml"]Paths to the resource actions files for Liferay Service Builder to use in generating the service layer. It sets the service.resource.actions.configs argument.
resourcesDirFilenullA directory where the service non-Java files are generated. It sets the service.resources.dir argument.
springFileFilenullA service Spring file to generate. It sets the service.spring.file argument.
springNamespacesSet["beans"]Namespaces of Spring XML Schemas to add to the service Spring file. It sets the service.spring.namespaces argument.
sqlDirFilenullA directory where the SQL files are generated. It sets the service.sql.dir argument.
sqlFileNameString"tables.sql"A name (relative to sqlDir) for the file in which the SQL table creation instructions are generated. It sets the service.sql.file argument.
sqlIndexesFileNameString"indexes.sql"A name (relative to sqlDir) for the file in which the SQL index creation instructions are generated. It sets the service.sql.indexes.file argument.
sqlSequencesFileNameString"sequences.sql"A name (relative to sqlDir) for the file in which the SQL sequence creation instructions are generated. It sets the service.sql.sequences.file argument.
targetEntityNameStringnullIf specified, it’s the name of the entity for which Liferay Service Builder should generate the service. It sets the service.target.entity.name argument.
testDirFilenullIf specified, it’s a directory where integration test Java source files are generated. It sets the service.test.dir argument.
uadDirFilenullA directory where the UAD (user-associated data) Java source files are generated. It sets the service.uad.dir argument.
uadTestIntegrationDirFilenullA directory where integration test UAD (user-associated data) Java source files are generated. It sets the service.uad.test.integration.dir argument.

The properties of type File supports any type that can be resolved by project.file. Moreover, it is possible to use Closures and Callables as values for the String properties, to defer evaluation until task execution.

Additional Configuration

There are additional configurations that can help you use Service Builder.

Liferay Service Builder Dependency

By default, the plugin creates a configuration called serviceBuilder and adds a dependency to the latest released version of Liferay Service Builder. It is possible to override this setting and use a specific version of the tool by manually adding a dependency to the serviceBuilder configuration:

dependencies {
    serviceBuilder group: "com.liferay", name: "com.liferay.portal.tools.service.builder", version: "1.0.292"
}

If you’re applying the com.liferay.gradle.plugins or com.liferay.gradle.plugins.workspace plugins to your project, the Service Builder dependency is already added to the serviceBuilder configuration. Therefore, if you try to apply a customized version of Service Builder, it’s not recognized; you must override the configuration already applied.

To do this, you must customize the classpath of the buildService task. If you’re supplying the customized Service Builder plugin through a module named custom-sb-api, you could modify the buildService task like this:

buildService {
    apiDir = "../custom-sb-api/src/main/java"
    classpath = configurations.serviceBuilder.filter { file -> !file.name.contains("com.liferay.portal.tools.service.builder") }
}

If you do this in conjunction with the serviceBuilder dependency configuration, the custom Service Builder version is used.

« REST Builder Gradle PluginSource Formatter Gradle Plugin »
この記事は役に立ちましたか?
1人中0人がこの記事が役に立ったと言っています