blob: 4f9066551c01f681f3f5b9daaa997a2f2909a740 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.6 at 2016-09-04 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache Rat™ Task Library for Apache Ant &#x2013; Custom Types</title>
<style type="text/css" media="all">
@import url("../css/maven-base.css");
@import url("../css/maven-theme.css");
@import url("../css/site.css");
</style>
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
<meta name="Date-Revision-yyyymmdd" content="20160904" />
<meta http-equiv="Content-Language" content="en" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Mate+SC" type="text/css"/>
</head>
<body class="composite">
<div id="banner">
<a href="https://www.apache.org/" id="bannerLeft">
<img src="https://www.apache.org/img/asf_logo.png" alt="The Apache Software Foundation" title="The Apache Software Foundation" />
</a>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
<span id="publishDate">Last Published: 2016-09-04</span>
&nbsp;| <span id="projectVersion">Version: 0.13-SNAPSHOT</span>
| <a href="https://www.apache.org/" class="externalLink" title="Apache">Apache</a>
&gt;
<a href="https://creadur.apache.org/" class="externalLink" title="Creadur">Creadur</a>
&gt;
<a href="https://creadur.apache.org/rat/" class="externalLink" title="Rat">Rat</a>
&gt;
<a href="../" title="Apache Rat™ Task Library for Apache Ant">Apache Rat™ Task Library for Apache Ant</a>
&gt;
Custom Types
</div>
<div class="xright">
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>Parent Project</h5>
<ul>
<li class="none">
<a href="../../index.html" title="Apache Creadur Rat">Apache Creadur Rat</a>
</li>
</ul>
<h5>Overview</h5>
<ul>
<li class="none">
<a href="../index.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="../report.html" title="Report Task">Report Task</a>
</li>
<li class="none">
<a href="../types.html" title="Types">Types</a>
</li>
</ul>
<h5>Examples</h5>
<ul>
<li class="none">
<a href="../examples/basic.html" title="Basic Usage">Basic Usage</a>
</li>
<li class="none">
<strong>Custom Types</strong>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="../project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="../project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<h5>Apache Rat™</h5>
<ul>
<li class="none">
<a href="../../index.html" title="Introducing Rat">Introducing Rat</a>
</li>
<li class="none">
<a href="../../apidocs/index.html" title="Javadocs">Javadocs</a>
</li>
<li class="none">
<a href="../../download_rat.cgi" title="Downloads">Downloads</a>
</li>
<li class="none">
<a href="../../RELEASE_NOTES.txt" title="Changes">Changes</a>
</li>
</ul>
<h5>Running Rat</h5>
<ul>
<li class="none">
<a href="../../apache-rat/index.html" title="From The Command Line">From The Command Line</a>
</li>
<li class="none">
<a href="../../apache-rat-tasks/index.html" title="With Ant">With Ant</a>
</li>
<li class="none">
<a href="../../apache-rat-plugin/index.html" title="With Maven">With Maven</a>
</li>
</ul>
<h5>Apache Creadur™</h5>
<ul>
<li class="none">
<a href="https://creadur.apache.org" class="externalLink" title="Creadur Project Home">Creadur Project Home</a>
</li>
<li class="none">
<a href="https://creadur.apache.org/tentacles" class="externalLink" title="Apache Tentacles">Apache Tentacles</a>
</li>
<li class="none">
<a href="https://creadur.apache.org/whisker" class="externalLink" title="Apache Whisker">Apache Whisker</a>
</li>
<li class="none">
<a href="https://www.apache.org/security/" class="externalLink" title="Security">Security</a>
</li>
<li class="none">
<a href="https://www.apache.org/licenses/" class="externalLink" title="License">License</a>
</li>
<li class="none">
<a href="https://www.apache.org/foundation/sponsorship.html" class="externalLink" title="Sponsorship">Sponsorship</a>
</li>
<li class="none">
<a href="https://www.apache.org/foundation/thanks.html" class="externalLink" title="Thanks">Thanks</a>
</li>
</ul>
<h5>The Apache Software Foundation</h5>
<ul>
<li class="none">
<a href="https://www.apache.org/foundation" class="externalLink" title="About the Foundation">About the Foundation</a>
</li>
<li class="none">
<a href="https://projects.apache.org" class="externalLink" title="The projects">The projects</a>
</li>
<li class="none">
<a href="https://people.apache.org" class="externalLink" title="The people">The people</a>
</li>
<li class="none">
<a href="https://www.apache.org/foundation/how-it-works.html" class="externalLink" title="How we work">How we work</a>
</li>
<li class="none">
<a href="https://www.apache.org/foundation/how-it-works.html#history" class="externalLink" title="Our history">Our history</a>
</li>
<li class="none">
<a href="https://blogs.apache.org/foundation/" class="externalLink" title="News">News</a>
</li>
</ul>
<h5>Contribute</h5>
<ul>
<li class="none">
<a href="https://www.apache.org/foundation/getinvolved.html" class="externalLink" title="Get Involved">Get Involved</a>
</li>
</ul>
<h5>Committer Info</h5>
<ul>
<li class="none">
<a href="https://www.apache.org/dev/committers.html" class="externalLink" title="ASF Committers' FAQ">ASF Committers' FAQ</a>
</li>
<li class="none">
<a href="https://www.apache.org/dev/new-committers-guide.html" class="externalLink" title="New Committers Guide">New Committers Guide</a>
</li>
<li class="none">
<a href="../../site-publish.html" title="Howto publish this site">Howto publish this site</a>
</li>
<li class="none">
<a href="https://planet.apache.org/committers/" class="externalLink" title="Plant Apache">Plant Apache</a>
</li>
<li class="none">
<a href="https://community.apache.org/" class="externalLink" title="Community">Community</a>
</li>
<li class="none">
<a href="https://www.apache.org/legal/" class="externalLink" title="Legal">Legal</a>
</li>
<li class="none">
<a href="https://www.apache.org/foundation/marks/" class="externalLink" title="Branding">Branding</a>
</li>
<li class="none">
<a href="https://www.apache.org/press/" class="externalLink" title="Media Relations">Media Relations</a>
</li>
</ul>
<a href="https://maven.apache.org/" title="Maven" class="poweredBy">
<img class="poweredBy" alt="Maven" src="https://maven.apache.org/images/logos/maven-feather.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --><!-- Licensed to the Apache Software Foundation (ASF) under one or more --><!-- contributor license agreements. See the NOTICE file distributed with --><!-- this work for additional information regarding copyright ownership. --><!-- The ASF licenses this file to You under the Apache License, Version 2.0 --><!-- (the "License"); you may not use this file except in compliance with --><!-- the License. You may obtain a copy of the License at --><!-- --><!-- http://www.apache.org/licenses/LICENSE-2.0 --><!-- --><!-- Unless required by applicable law or agreed to in writing, software --><!-- distributed under the License is distributed on an "AS IS" BASIS, --><!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --><!-- See the License for the specific language governing permissions and --><!-- limitations under the License. --><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --><div class="section">
<h2><a name="Custom_Types"></a>Custom Types</h2>
<p>It is possible to provide license matchers or license families of your own by implementing one of the Rat interfaces and providing the implementation as an Ant type. This example will show how to use a custom license matcher.</p>
<p>To be suitable as an Ant type your class must provide a public no-arg constructor (or no constructor at all). If you want to support configuration of your instance via XML attributes or nested elements your type must provide methods that follow Ant's method naming conventions. For details see the <a class="externalLink" href="http://ant.apache.org/manual/develop.html#writingowntask"> Writing Your Own Task</a> section in Ant's manual.</p>
<p>For this example let us assume you want to match a license that looks like</p>
<div>
<pre> /**
* Yet Another Software License, 1.0
*
* Lots of text, specifying the users rights, and whatever ...
*/</pre></div>
<p>and to make things a bit more interesting the version number shall be configurable as an attribute on your type.</p>
<p>An implementation of this matcher could look like</p>
<div>
<pre>package org.example;
import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.analysis.RatHeaderAnalysisException;
import org.apache.rat.api.Document;
import org.apache.rat.api.MetaData;
import org.apache.rat.api.MetaData.Datum;
/** Just a POJO with no constructor at all */
public class YASLMatcher implements IHeaderMatcher {
private final String MATCH = &quot;Yet Another Software License, &quot;;
private final String FAMILY = &quot;YASL&quot;;
private final String ONE = &quot;1.0&quot;;
private String matchWithVersion = MATCH + ONE;
private String familyWithVersion = FAMILY + ONE;
/** becomes the version attribute */
public void setVersion(String v) {
matchWithVersion = MATCH + v;
familyWithVersion = FAMILY + v;
}
public void reset() { }
/** trivial implementation that looks for a fixed string */
public boolean match(Document subject, String line)
throws RatHeaderAnalysisException {
if (line.indexOf(matchWithVersion) &gt;= 0) {
// this is what matchers are supposed to do when they find a
// match, even though it is not documented very well
subject.getMetaData().set(new Datum(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY,
familyWithVersion));
return true;
}
return false;
}
}</pre></div>
<p>Actually it would be easier to use Rat's <tt>SimplePatternBasedLicense</tt> implementation or the built-in <a href="../types.html#substringMatcher"><tt>substringMatcher</tt></a>, but this is just an example.</p>
<p>In order to use your matcher it has to be defined as an Ant type. You do so by either using <a class="externalLink" href="http://ant.apache.org/manual/Tasks/typedef.html"> <tt>typedef</tt></a> or <a class="externalLink" href="http://ant.apache.org/manual/Tasks/componentdef.html"> <tt>componentdef</tt></a>. If you are using Ant 1.8.x you should use <tt>componentdef</tt>.</p>
<p>In the most simple case this means</p>
<div>
<pre>&lt;componentdef name=&quot;yasl&quot;
classname=&quot;org.example.YASLMatcher&quot;/&gt;</pre></div>
<p>Since you will be using classes from Rat's core in both the Report task and your implementation you must ensure they get loaded via the same classloader. If you are using the <tt>-lib</tt> command line switch Rat's core will be part of Ant's core classloader and you don't need to reference it in the <tt>typedef</tt> for <tt>yasl</tt> at all.</p>
<p>If you define the Rat Antlib via a <tt>taskdef</tt> task with a nested <tt>classpath</tt> then you just use the same <tt>classpath</tt> when defining your custom matcher as well and set <tt>loaderRef</tt> to the same name. I.e.</p>
<div>
<pre>&lt;path id=&quot;rat-classpath&quot;&gt;
&lt;pathelement location=&quot;YOUR-PATH-TO/apache-rat-${version}.jar&quot;/&gt;
&lt;pathelement location=&quot;YOUR-PATH-TO/yasl-matcher.jar&quot;/&gt;
&lt;/path&gt;
&lt;taskdef
classpathref=&quot;rat-classpath&quot;
loaderref=&quot;rat-classloader&quot;
resource=&quot;org/apache/rat/anttasks/antlib.xml&quot;&gt;
&lt;/taskdef&gt;
&lt;componentdef name=&quot;yasl&quot;
classpathref=&quot;rat-classpath&quot;
loaderref=&quot;rat-classloader&quot;
classname=&quot;org.example.YASLMatcher&quot;/&gt;</pre></div>
<p>With a definition like this you can use your matcher as in</p>
<div>
<pre>&lt;report&gt;
&lt;fileset dir=&quot;src&quot;/&gt;
&lt;yasl/&gt;
&lt;/report&gt;</pre></div>
<p>to detect Version 1.0 or</p>
<div>
<pre>&lt;report&gt;
&lt;fileset dir=&quot;src&quot;/&gt;
&lt;yasl version=&quot;2.0&quot;/&gt;
&lt;/report&gt;</pre></div>
<p>for 2.0.</p></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
<?xml version="1.0" encoding="UTF-8"?>
<footer>Copyright © 2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Apache Creadur, Creadur, Apache Rat, Apache Tentacles, Apache Whisker, Apache and the Apache feather logo are trademarks
of The Apache Software Foundation.
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.</footer>
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>