ALFA Maven Plugin¶
ALFA Tools includes an Apache Maven plugin that supports compiling ALFA projects and executing ALFA model exporters.
Plugin installation¶
There are 3 possible ways of installing the ALFA Maven Plugin.
1. Reference alfa-lang.io repository¶
The ALFA Maven plugin is published at
https://alfa-lang.io/repository
and it can be referenced by specifying arepositories
block in thepom.xml
.Add the following repository configuration to your pom.xml file for Maven to locate the ALFA Plugin and runtime.
<!-- For the ALFA Maven Plugin /--> <pluginRepositories> <pluginRepository> <id>alfa-repo</id> <url>https://alfa-lang.io/repository</url> </pluginRepository> </pluginRepositories> <!-- For the ALFA Java Runtime libraries /--> <repositories> <repository> <id>alfa-repo</id> <url>https://alfa-lang.io/repository</url> </repository> </repositories>
2. Install to local repository¶
In corporate environments, developer machines and build servers may not have access to external repositories. You may can manually install the plugin to your local repository.
The ALFA Maven plugin is included in the AlfaPackage-2.5.1.zip file in the
mvn-repository
directory. Once the AlfaPackage-2.5.1.zip is downloaded to a share location, the contents of themvn-repository
can be installed to your local repository.The commands below will install both the ALFA Maven Plugin and the ALFA Java runtime (required to compile generated Java code).
unzip AlfaPackage-2.5.1.zip cd mvn-repository mvn org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \ -Dfile=io/alfa/utils/alfa-maven-plugin/2.5.1/alfa-maven-plugin-2.5.1.jar \ -DpomFile=io/alfa/utils/alfa-maven-plugin/2.5.1/alfa-maven-plugin-2.5.1.pom mvn org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \ -Dfile=io/alfa/rt/alfa-rt-java-core/2.5.1/alfa-rt-java-core-2.5.1.jar \ -DpomFile=io/alfa/rt/alfa-rt-java-core/2.5.1/alfa-rt-java-core-2.5.1.pom
Plugin Usage¶
An example usage of the plugin is shown below:
<plugin>
<groupId>io.alfa.utils</groupId>
<artifactId>alfa-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>alfa-exec</id>
<goals>
<goal>compile</goal>
<goal>package</goal>
</goals>
<configuration>
<exportSettings>
<exportSetting>
<exportType>java</exportType>
</exportSetting>
</exportSettings>
</configuration>
</execution>
</executions>
</plugin>
The plugin support 2 Maven goals, compile and package.
- compile - compiles and executes exporters defined under configuration.
- package - packages the alfa files into a zip artifact, that can be uploaded to a repository and be used as dependency to other ALFA projects.
The plugin locates ALFA source files in 3 possible ways.
Top level Maven source directory
<sourceDirectory>${basedir}/src/main/alfa</sourceDirectory>
Use
build-helper-maven-plugin
and addsource
directory<source>${basedir}/src/main/alfa</source>
Specify
srcPath
parameter to the ALFA plugin as shown in the complete example below. ..<srcPath>${basedir}/src/main/alfa</srcPath>
Plugin Parameters¶
All parameters are optional.
Name | Description |
---|---|
srcPath | Custom path to ALFA source files. |
srcDependency | Specified as < group >:< artifact >:< version >; Use the dependency as source for code generation. Plugin should depend on the required artifact. |
exportSetting[] | Array of ExportSetting objects |
exportSetting.exportType | Exporter to run - e.g. java |
exportSetting.outputDir | Custom output dir. By default target/generated-sources/${exportType} |
exportSetting.config | Map of key value pairs for configuring exporter. |
Plugin Compile-time Dependencies¶
ALFA files being compiled by the Plugin can reference other Maven-based artifacts at compile time. This allows teams to share their definitions via formal release processes. Downstream teams can express dependencies on ALFA artifacts to reference definitions from other ALFA artifacts.
Other ALFA generated projects can be used a dependencies by specifying a dependency when using the ALFA plugin.
Note the classifier
and type
fields need to be specified as a alfa
and zip
respectively.
<dependencies>
<dependency>
<groupId>other.project.group</groupId>
<artifactId>other-project-name</artifactId>
<version>1.0</version>
<classifier>alfa</classifier>
<type>zip</type>
</dependency>
</dependencies>
Dependencies can used as source for code generation. See srcDependency
plugin parameter.
Complete Example¶
To quickly setup a test Maven project with ALFA files, you can follow the 3 steps below.
- Create a new directory
alfademo
. Inalfademo
, create new filepom.xml
file and paste the contents below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>alfa-demo-project</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <encoding>UTF-8</encoding> </properties> <!-- The generated Java code depends on the the alfa-rt-java-core library /--> <dependencies> <dependency> <groupId>io.alfa.rt</groupId> <artifactId>alfa-rt-java-core</artifactId> <version>2.5.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>io.alfa.utils</groupId> <artifactId>alfa-maven-plugin</artifactId> <version>2.5.1</version> <executions> <execution> <id>alfa-exec</id> <goals> <goal>compile</goal> <goal>package</goal> </goals> <configuration> <!-- source path /--> <srcPath>${basedir}/src/main/alfa</srcPath> <!-- generate java /--> <exportSettings> <exportSetting> <exportType>java</exportType> </exportSetting> </exportSettings> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>add-java-gen-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/java</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build> <!-- Uncomment below to download ALFA artifacts from alfa-lang.io/repository /--> <!-- <repositories> <repository> <id>alfa-repo</id> <url>https://alfa-lang.io/repository</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>alfa-repo</id> <url>https://alfa-lang.io/repository</url> </pluginRepository> </pluginRepositories> /--> </project>
- In
alfademo
, create a file in a subdirectory assrc/main/alfa/demo.alfa
, and paste the contents below.
namespace sample trait Person { FirstName : string LastName : string DateOfBirth : date email : string } enum ServiceLevelType { Standard Gold Platinum } entity Customer key ( Id : uuid ) includes Person { Accounts : set< string > DeclaredIncomes : list< double > ServiceLevel : ServiceLevelType } service CustomerSvc() { getCustomerByEmailAddress( email : string) : Customer? }
- Run
mvn install
in thealfademo
directory. When run the 1st time, it will download the ALFA plugin and runtime arifacts.
If successful, you should have jar created -
target/alfa-demo-project-1.0-SNAPSHOT.jar
. The generated Java code can be seen intarget/generated-sources/java/
.Congratulations! You have generated and built your 1st ALFA type library!