Skip to content

Datum Capture Part 4

Matt Magoffin edited this page Apr 23, 2023 · 4 revisions

SolarNode Datum Capture Plug-in Example, Part 4

This guide explains how to add command line build support to the Foobar Power SolarNode plug-in. If you have not read part three you will want to go back and follow that part first before continuing here.

The source for this part of the guide is available as well.

In this part we will:

  1. Add an Apache Ant build script
  2. Add an Apache Ivy configuration

Get Apache Ant

If you do not already have Apache Ant installed, by all means, install that now before continuing.

Ant build script

You should have already followed the Eclipse Setup Guide and have the solarnetwork-build repo cloned and available in the same directory that you cloned the solarnetwork-node repo into. The solarnetwork-build repo contains some helper Ant scripts that make an individual plug-in's Ant script trivial to implement.

Create a build.xml file at the top level of your plug-in's directory. In it, place the following content:

<project basedir=".">

	<property name="dir.osgi.base" value="${basedir}/../../solarnetwork-build/solarnetwork-osgi-lib"/>
	<import file="${dir.osgi.base}/lib-build.xml"/>

</project>

The only part you need to worry about changing is that dir.osig.base value, which should point to the solarnetwork-build/solarnetwork-osgi-lib directory, so that lib-build.xml file can be imported correctly on the next line.

Now you should be able to run ant -projecthelp to see a list of common tasks.

$ ant -projecthelp

Buildfile: net.solarnetwork.node.example.datum-capture/build.xml

Main targets:

 clean            Remove generated artifacts
 clean-full       Remove generated artifacts and in dependent projects
 clean-jar        Clean, then create bundle jar
 clean-test       Build and run unit tests from scratch
 coverage-report  Generate a HTML code coverage report from the test results
 jar              Create bundle jar
 javadoc          Generate Javadoc documentation.
 publish          Publish to artifact repository
 resource-prep    Prepare WAB files
 src-jar          Create source bundle jar
 stage            Publish to staging OSSRH repository
 test             Run unit tests
 test-report      Generate a HTML report from test results

The jar task is the one we are interested the most in. That task will compile the plug-in classes and create a proper OSGi bundle JAR out of them. Thus, you simply run ant jar to produce your finished plug-in.

Ivy configuration

Wait, did you try to run ant jar already? That will not work yet, because we have not also provided an Ivy file that desribes what other JARs we need to compile our code with. Create an ivy.xml file, right next to that build.xml file, with the following content:

<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven">
	<info organisation="net.solarnetwork.node" module="${bundle.name}" />
	<configurations defaultconf="compile,runtime">
		<conf name="compile" visibility="public" description="Build dependencies"/>
		<conf name="runtime" visibility="public" description="Runtime dependencies"/>
		<conf name="javadoc" visibility="public" description="Javadoc documentation"/>
		<conf name="sources"/>
	</configurations>
	<publications>
		<artifact type="pom" ext="pom"/>
		<artifact type="bundle" ext="jar"/>
		<artifact type="javadoc" ext="jar" conf="javadoc" m:classifier="javadoc"/>
		<artifact type="sources" ext="jar" conf="sources" m:classifier="sources"/>
	</publications>
	<dependencies defaultconfmapping="runtime->default(runtime);compile->default(compile)">
		<dependency org="net.solarnetwork.common" name="net.solarnetwork.common" rev="[3.0,)"/>
		<dependency org="net.solarnetwork.node" name="net.solarnetwork.node" rev="[3.0,)"/>
		<dependency org="org.slf4j" name="slf4j-api" rev="1.7.32"/>
		<dependency org="org.springframework" name="spring-context-support" rev="5.3.24"/>
	</dependencies>
</ivy-module>

The interesting section here is <dependencies>, where the compile-time dependencies are listed.

Build

Now you should be able to run ant jar and succeed. The first time you do this it might take a while as Ivy has to go out and download all those dependencies. Subsequent builds will be much, much faster.

$ ant jar

Buildfile: net.solarnetwork.node.example.datum-capture/build.xml

...

compile:
     [echo] [====> Compiling net.solarnetwork.node.example.datum-capture <====]

resource-prep:

jar.classes:
     [echo] [====> Building JAR net.solarnetwork.node.example.datum-capture/target/net.solarnetwork.node.example.datum-capture-1.0.0.jar <====]

jar.no.classes:

jar:

BUILD SUCCESSFUL
Total time: 1 second

Yay, that worked! The resulting JAR is shown at the end, it will appear as target/net.solarnetwork.node.example.datum-capture-1.0.0.jar in the plug-in directory.

Conclusion

That's the end of this guide. You will find many, many example datum data source (and other) plug-ins in the SolarNode repository. Happy coding!

🎉

Clone this wiki locally