Generating code in Maven projects with the AlphaSimple plug-in

This just in: you can now generate code for AlphaSimple projects from within your Maven-based project build! That gives you a convenient way of getting the code generated by AlphaSimple into your (and your teammates’) development environment, or in your automated builds.

How do you do that? Let’s see.

Step 0: create your model(s) and template(s)

You must have an existing project in AlphaSimple. This was the subject of a previous post. Read it first if you don’t know how to create models and templates in AlphaSimple. Make sure your project is shared.

Feeling lazy?

Okay… just copy and paste the pom definition from this file into your pom.xml. You can skip down to step 3, and it will work out of the box (generating code from a pre-existing shared project).

Step 1: Enable the Abstratt repository

<project ...>
...
<pluginRepositories>
...
  <pluginRepository>
    <id>abstratt</id>
    <name>Abstratt Technologies Maven repository</name>
    <url>http://abstratt.com/maven/</url>
  </pluginRepository>
...
</pluginRepositories>
...
<project>

This is required because the AlphaSimple Maven plug-in is not available from Maven Central or other public repository (yet).

Step 2: Include the AlphaSimple Maven plugin in your pom.xml

<project ...>
...
<build>
...
<plugins>
...
  <plugin>
    <groupId>com.abstratt</groupId>
    <artifactId>com.abstratt.alphasimple.mojo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
      <uri>http://cloudfier.com/alphasimple/mdd/generator/rafael-276/simple</uri>
      <targetRoot>${project.build.directory}/generated-src/main/java</targetRoot>
    </configuration>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
...
</plugins>
...
</build>
...
</project>

Which in summary is executing the generate goal of the AlphaSimple plugin during the generate-sources phase of the Maven lifecycle.

In the example above, the plug-in is configured to execute the generator at http://cloudfier.com/alphasimple/mdd/generator/rafael-276/simple, for the AlphaSimple sample project (see this post for how to obtain a similar URI for your own project).

Also, files will be generated at the specified location (which in the example above will map to target/generated-src/main/java). In order for them to be seen by the Java compiler, that location must be configured as a source directory, for instance, by specifying a non-standard source location in your module:

<project ...>
...
<build>
  ...
  <sourceDirectory>${project.build.directory}/generated-src/main/java</sourceDirectory>
  ...
</build>
...
</project>

Step 3: run Maven

    mvn clean generate-sources

will get you something like this:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building AlphaSimple Code Generation example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.3:clean (default-clean) @ com.alphasimple.examples.pojo ---
[INFO] Deleting file set: C:devcom.alphasimple.examples.pojotarget (included: [**], excluded: [])
[INFO] 
[INFO] --- com.abstratt.alphasimple.mojo:1.0-SNAPSHOT:generate (default) @ com.alphasimple.examples.pojo ---
[INFO] Generating at C:devcom.alphasimple.examples.pojotargetgenerated-srcmainjavaalphasimpleProject.java
[INFO] Generating at C:devcom.alphasimple.examples.pojotargetgenerated-srcmainjavaalphasimpleSession.java
[INFO] Generating at C:devcom.alphasimple.examples.pojotargetgenerated-srcmainjavaalphasimpleUnit.java
[INFO] Generating at C:devcom.alphasimple.examples.pojotargetgenerated-srcmainjavaalphasimpleUser.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.486s
[INFO] Finished at: Sun Mar 06 16:48:36 PST 2011
[INFO] Final Memory: 2M/58M
[INFO] ------------------------------------------------------------------------

The generate-sources phase or any other phase that follows (such as compile, package, install etc see lifecycle reference) will cause the code to be regenerated. As you make changes to your models or templates in AlphaSimple, further runs of the generate goal will take those changes into account.

In the case of generating Java code, you will want to include at least the compile phase so you can tell whether the generated code is valid (if you get an error about generics not allowed in source level 3, see this).

What just happened?

The AlphaSimple Maven plugin does not know how to generate code, nor has dependencies on other Maven artifacts that do. All it does is to hit the code generation endpoint in the AlphaSimple REST API, and request code to be generated for the chosen target platform. It then just extracts that ZIP stream into the chosen location in the file system.

Conclusion
Once you create your models and templates in AlphaSimple (see previous post), it is very easy to include the generated code in your Maven-based projects. All you need to do is to include an execution of the AlphaSimple plug-in and point it to the generator of your choice. It is that easy. But don’t take our word for it, try it yourself and give us your opinion!

EmailFacebookLinkedInGoogle+Twitter

Comments are closed.