Skip to content

Latest commit

 

History

History

maven-plugins

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Maven Plugins

Dotty project Directory maven-plugins\ contains Maven plugin examples found on the Web or written by ourself.
Our final objective to compile Scala source files from a POM file using the Maven command line tool.

In the past years at least two Maven plugins for Scala have been developed, i.e. David's scala-maven-plugin and the outdated plugin maven-scala-plugin (February 2011).

We were no satisfied with the above plugins and decided in late 2018 to write our own Maven plugin for compiling 1 the Scala source files of our Scala projects (see for instance examples\README.md).

Plugin naming convention
The Maven Guide to Developing Java Plugins starts with an important notice that recommand us to name our plugin <yourplugin>-maven-plugin.

first-maven-plugin

This plugin project is adapted from the code example presented in section 11.4 – Writing a Custom Plugin – of the online book Maven: The Complete Reference.

The project is organized as follows:

> tree /a /f . |findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   pom.xml
|
+---src
    \---main
        \---java
            \---org
                \---sonatype
                    \---plugins
                             EchoMojo.java

WIP 2

hello-maven-plugin

The plugin project hello-maven-plugin is the code example presented in the online Maven Guide to Developing Java Plugins.

The project is organized as follows :

> tree /a /f . |findstr /v /b [A-Z]
|   00download.txt
|   install-local.bat
|   pom.xml
|
+---src
|   \---main
|       +---java
|           \---sample
|               \---plugin
|                       GreetingMojo.java
\---test
    |   test\pom.xml
    \---src
        \---main
            +---java
                    Dummy.java

WIP

scala-maven-plugin

We started this project in late 2018; we use scala-maven-plugin in all Scala projects of our GitHub repository michelou/dotty-examples (but not only).

scala-maven-plugin features two goals with parameters similar to the maven-compiler-plugin:

Goal Parameters
compile additionalClasspathElements
addOutputToClasspath
compilerArgs
excludes
includes
run jvmArgs

The project is organized as follows :

> tree /a /f . |findstr /v /b [A-Z]
|   pom.xml
|
+---src
    \---main
        \---java
            \---ch
                \---epfl
                    \---alumni
                            HelpMojo.java
                            ScalaAbstractMojo.java
                            ScalaCompileMojo.java
                            ScalaRunMojo.java

Command mvn package generates the JAR file scala-maven-plugin-1.0.0.jar to be deployed as Maven artifact :

> mvn clean compile package
[INFO] Scanning for projects...
[...]
[INFO] java-javadoc mojo extractor found 0 mojo descriptor.
[INFO] bsh mojo extractor found 0 mojo descriptor.
[INFO] ant mojo extractor found 0 mojo descriptor.
[INFO] java-annotations mojo extractor found 3 mojo descriptors.
[...]
 
> dir /b target\*.jar
scala-maven-plugin-1.0.0.jar

Command mvn deploy:deploy-file installs the Maven artifact for our plugin into our local Maven repository %USERPROFILE%\.m2\repository\ :

> mvn deploy:deploy-file -DgroupId=ch.epfl.alumni -DartifactId=scala-maven-plugin -Dversion=1.0.0 -Durl=file://%USERPROFILE%/.m2/repository -DupdateReleaseInfo=true -Dfile=.\target\scala-maven-plugin-1.0.0.jar -Dpackaging=jar -DpomFile=.\pom.xml -DgeneratePom=true -DcreateChecksum=true
[INFO] Scanning for projects...
[...]

scala-maven-plugin Installation
A user has currently two possibilities to install scala-maven-plugin:

  • Download our source project, generate and install the plugin as described above.
  • Download and extract the Zip file scala-maven-plugin-1.0.zip into the local Maven repository.

The plugin installation should look as follows :

> tree /a /f %USERPROFILE%\.m2\repository\ch\epfl\alumni |findstr /v /b [A-Z]
\---scala-maven-plugin
    |   maven-metadata-remote-repository.xml
    |   maven-metadata-remote-repository.xml.sha1
    |   maven-metadata.xml
    |   maven-metadata.xml.md5
    |   maven-metadata.xml.sha1
    |   resolver-status.properties
    |
    \---1.0.0
            scala-maven-plugin-1.0.0.jar
            scala-maven-plugin-1.0.0.jar.md5
            scala-maven-plugin-1.0.0.jar.sha1
            scala-maven-plugin-1.0.0.pom
            scala-maven-plugin-1.0.0.pom.md5
            scala-maven-plugin-1.0.0.pom.sha1

Footnotes

[1] pom.xml

We give here the <plugin> section of a POM file.
<plugin>
    <groupId>ch.epfl.alumni</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>${scala.maven.version}</version>
    <executions>
        <execution>
            <id>scala-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${m2.hamcrest.jar}</additionalClasspathElement>
                    <!-- ...more additions.. -->
                </additionalClasspathElements>
                <includes>
                    <include>scala/**/*.scala</include>
                </includes>
                <installDirectory>${env.SCALA3_HOME}
            </configuration>
        </execution>
        <!-- ...execution of "run" goal... -->
    </executions>
    <configuration>
        <scalaVersion>${scala.version}</scalaVersion>
        <localInstall>${scala.local.install}</localInstall>
        <!-- <debug>true</debug> -->
        <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
        </jvmArgs>
    </configuration>
</plugin>

[2] META-INF/maven/plugin.xml

<plugin>
  <name>Sample Parameter-less Maven Plugin</name>
  <description></description>
  <groupId>sample.plugin</groupId>
  <artifactId>hello-maven-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <goalPrefix>hello</goalPrefix>
  <isolatedRealm>false</isolatedRealm>
  <inheritedByDefault>true</inheritedByDefault>
  <mojos>
    <mojo>
      <goal>sayhi</goal>
      <description>Says "Hi" to the user.</description>
      <requiresDirectInvocation>false</requiresDirectInvocation>
      <requiresProject>true</requiresProject>
      <requiresReports>false</requiresReports>
      <aggregator>false</aggregator>
      <requiresOnline>false</requiresOnline>
      <inheritedByDefault>true</inheritedByDefault>
      <implementation>sample.plugin.GreetingMojo</implementation>
      <language>java</language>
      <instantiationStrategy>per-lookup</instantiationStrategy>
      <executionStrategy>once-per-session</executionStrategy>
      <threadSafe>false</threadSafe>
      <parameters/>
    </mojo>
  </mojos>
  <dependencies/>
</plugin>

mics/April 2023