add configuration tutorials

git-svn-id: https://svn.apache.org/repos/asf/incubator/ivy/trunk@484140 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
mm 2006-01-23 17:01:28 +00:00
parent 2ddba57e03
commit ec2825f61b
17 changed files with 536 additions and 0 deletions

View File

@ -0,0 +1,79 @@
<project name="configurations" default="run.dev" xmlns:ivy="antlib:fr.jayasoft.ivy.ant">
<!-- some variables used -->
<property name="lib.dir" value="lib" />
<property name="build.dir" value="build" />
<property name="src.dir" value="src" />
<!-- paths used for compilation and run -->
<path id="compile.path.id">
<fileset dir="${lib.dir}/compile" />
</path>
<path id="lib.run.dev.id">
<path location="${build.dir}" />
<fileset dir="${lib.dir}/rundev" />
</path>
<path id="lib.run.prod.id">
<path location="${build.dir}" />
<fileset dir="${lib.dir}/runprod" />
</path>
<!-- =================================
target: resolve
================================= -->
<target name="resolve" description="--> retreive dependencies with ivy">
<!-- conf="*" will copie artifacts defined for each conf in a dir matching conf name -->
<ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
</target>
<!-- =================================
target: report
================================= -->
<target name="report" depends="resolve" description="--> generates a report of dependencies">
<ivy:report todir="${build.dir}"/>
</target>
<!-- =================================
target: run.dev
================================= -->
<target name="run.dev" depends="resolve" description="--> compile and run the project">
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.run.dev.id" />
<copy todir="${build.dir}">
<fileset dir="${src.dir}" includes="**/*.properties"></fileset>
</copy>
<java classpathref="lib.run.dev.id" classname="example.ConfigurationsExample" fork="true">
<arg value="--dev"/>
</java>
</target>
<!-- =================================
target: run.prod
================================= -->
<target name="run.prod" depends="resolve" description="--> compile and run the project">
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.run.prod.id"/>
<copy todir="${build.dir}">
<fileset dir="${src.dir}" includes="**/*.properties"></fileset>
</copy>
<java classpathref="lib.run.prod.id" classname="example.ConfigurationsExample" fork="true" />
</target>
<!-- =================================
target: clean
================================= -->
<target name="clean" description="--> clean the project">
<delete includeemptydirs="true">
<fileset dir="${basedir}">
<exclude name="src/**" />
<exclude name="build.xml" />
<exclude name="ivy.xml" />
</fileset>
</delete>
</target>
<!-- =================================
target: clean-cache
================================= -->
<target name="clean-cache" description="--> clean the ivy cache">
<delete dir="${user.home}/.ivy-cache"/>
</target>
</project>

View File

@ -0,0 +1,26 @@
<?xml-stylesheet type="text/xsl" href="http://www.ivyrep.org/ivy-doc.xsl"?>
<ivy-module version="1.0">
<info organisation="jayasoft" module="configurations" >
<description>
This is an example project that aims to demonstrate the usage of the configuration in ivy.
This project provide 4 configurations. Each configurations describe the requirement to build or run the project
</description>
</info>
<configurations>
<conf name="compile" description="This is this configuration that describes modules need to build our project"/>
<conf name="test" extends="compile" description="This is this configuration that describes modules need to run test on our project"/>
<conf name="rundev" extends="compile" description="This is this configuration that describes modules need to execute our project in a dev environement"/>
<conf name="runprod" extends="compile" description="This is this configuration that describes modules need to execute our project in a production environement"/>
</configurations>
<dependencies>
<!-- this dependency is needed for all configuration -->
<dependency org="apache" name="commons-cli" rev="1.0" />
<!-- when launching our app in dev mode we use mckoi db and mckoi jdbc client conf="run.dev->embedded, client"-->
<dependency org="mckoi" name="mckoi" rev="1.0.2" conf="rundev->default"/>
<!-- when launching our app in production environement we needs other jdbc driver -->
<dependency org="mm-mysql" name="mm-mysql" rev="2.0.7" conf="runprod->default"/>
<!-- junit is only need in the test configuration-->
<dependency org="junit" name="junit" rev="3.8" conf="test->default"/>
</dependencies>
</ivy-module>

View File

@ -0,0 +1 @@
driver.class=com.mckoi.JDBCDriver

View File

@ -0,0 +1,44 @@
package example;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
public class ConfigurationsExample {
public static void main(String[] args) {
String jdbcPropToLoad = "prod.properties";
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("d", "dev", false, "Dev tag to launch app in dev mode. Means that app will launch embedded mckoi db.");
try {
CommandLine line = parser.parse( options, args );
if(line.hasOption("d")) {
System.err.println("App is in DEV mode");
jdbcPropToLoad = "dev.properties";
}
}
catch( ParseException exp ) {
System.err.println( "Parsing failed. Reason: " + exp.getMessage() );
}
Properties p = new Properties();
try {
p.load(ConfigurationsExample.class.getResourceAsStream("/"+jdbcPropToLoad));
} catch (IOException e) {
System.err.println( "Properties loading failed. Reason: " + e.getMessage());
}
try {
String clazz = p.getProperty("driver.class");
Class.forName(clazz);
System.out.println(" Jdbc driver loaded :"+clazz);
} catch (ClassNotFoundException e) {
System.err.println( "Jdbc Driver class loading failed. Reason: " + e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -0,0 +1 @@
driver.class=org.gjt.mm.mysql.Driver

View File

@ -0,0 +1,112 @@
<project name="filter-framework" default="publish" xmlns:ivy="antlib:fr.jayasoft.ivy.ant">
<!-- some variables used -->
<property name="lib.dir" value="lib" />
<property name="build.dir" value="build" />
<property name="distrib.dir" value="distrib" />
<property name="src.dir" value="src" />
<property name="test.dir" value="test" />
<property name="build.test.dir" value="${build.dir}/test-classes" />
<property name="report.test.dir" value="${build.dir}/test-report" />
<property name="revision" value="1.3" />
<!-- paths used for compilation and run -->
<path id="compile.path.id">
<fileset dir="${lib.dir}/cc-Impl" />
</path>
<path id="test.path.id">
<path location="${build.dir}" />
<path location="${build.test.dir}" />
<fileset dir="${lib.dir}/test" />
</path>
<!-- =================================
target: resolve
================================= -->
<target name="resolve" description="--> retreive dependencies with ivy">
<!-- conf="*" will copie artifacts defined for each conf in a dir matching conf name -->
<ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
</target>
<!-- =================================
target: build
================================= -->
<target name="build" depends="clean, resolve" description="--> compile and jar project">
<mkdir dir="${build.dir}" />
<mkdir dir="${distrib.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="compile.path.id"/>
<jar destfile="${distrib.dir}/filter-api.jar" >
<fileset dir="${build.dir}">
<include name="filter/*.class"/>
</fileset>
</jar>
<jar destfile="${distrib.dir}/filter-hmimpl.jar" >
<fileset dir="${build.dir}">
<include name="filter/hmimpl/*.class"/>
</fileset>
</jar>
<jar destfile="${distrib.dir}/filter-ccimpl.jar" >
<fileset dir="${build.dir}">
<include name="filter/ccimpl/*.class"/>
</fileset>
</jar>
</target>
<!-- =================================
target: test
================================= -->
<target name="test" depends="build" description="--> compile and test the project">
<mkdir dir="${report.test.dir}"/>
<mkdir dir="${build.test.dir}"/>
<javac srcdir="${test.dir}" destdir="${build.test.dir}" classpathref="test.path.id"/>
<junit printsummary="yes" fork="yes" haltonfailure="yes" >
<classpath refid="test.path.id"/>
<formatter type="plain"/>
<batchtest todir="${report.test.dir}" >
<fileset dir="${build.test.dir}">
<include name="**/**Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
<!-- =================================
target: publish
================================= -->
<target name="publish" depends="test" description="--> compile test and publish this project in the local ivy repository">
<property name="revision" value="${revision}"/>
<ivy:publish artifactspattern="${distrib.dir}/[artifact].[ext]"
resolver="local"
pubrevision="${revision}"
status="release"/>
<echo message="project ${ant.project.name} released with version ${revision}" />
</target>
<!-- =================================
target: clean
================================= -->
<target name="clean" description="--> clean the project">
<delete includeemptydirs="true">
<fileset dir="${basedir}">
<exclude name="src/**" />
<exclude name="test/**" />
<exclude name="build.xml" />
<exclude name="ivy.xml" />
<exclude name=".*" />
</fileset>
</delete>
</target>
<!-- =================================
target: clean-cache
================================= -->
<target name="clean-cache" description="--> clean the ivy cache">
<delete dir="${user.home}/.ivy/cache"/>
</target>
<!-- =================================
target: report
================================= -->
<target name="report" depends="resolve" description="--> generates a report of dependencies">
<ivy:report todir="${build.dir}"/>
</target>
</project>

View File

@ -0,0 +1,18 @@
<ivy-module version="1.0">
<info organisation="jayasoft" module="filter-framework"/>
<configurations>
<conf name="api" description="only provide filter framework API"/>
<conf name="homemade-impl" extends="api" description="provide a home made implementation of our api"/>
<conf name="cc-impl" extends="api" description="provide an implementation that use apache common collection framework"/>
<conf name="test" extends="cc-impl" visibility="private" description="for testing our framework"/>
</configurations>
<publications>
<artifact name="filter-api" type="jar" conf="api" ext="jar"/>
<artifact name="filter-hmimpl" type="jar" conf="homemade-impl" ext="jar"/>
<artifact name="filter-ccimpl" type="jar" conf="cc-impl" ext="jar"/>
</publications>
<dependencies>
<dependency org="apache" name="commons-collections" rev="3.1" conf="cc-impl->default"/>
<dependency org="junit" name="junit" rev="3.8" conf="test->default"/>
</dependencies>
</ivy-module>

View File

@ -0,0 +1,20 @@
package filter;
public class FilterProvider {
public static IFilter getFilter() {
try {
Class clazz = Class.forName("filter.ccimpl.CCFilter");
return (IFilter) clazz.newInstance();
} catch (Exception e) {
try {
Class clazz = Class.forName("filter.hmimpl.HMFilter");
return (IFilter) clazz.newInstance();
} catch (Exception e1) {
System.err.println("No filter implementation found in classpath !");
}
return null;
}
}
}

View File

@ -0,0 +1,5 @@
package filter;
public interface IFilter {
String[] filter(String[] values, String prefix);
}

View File

@ -0,0 +1,28 @@
package filter.ccimpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import filter.IFilter;
public class CCFilter implements IFilter {
public String[] filter(String[] values, final String prefix) {
if(values == null) {
return null;
}
if(prefix == null) {
return values;
}
List result = new ArrayList(Arrays.asList(values));
CollectionUtils.filter(result, new Predicate() {
public boolean evaluate(Object o) {
return o!= null && o.toString().startsWith(prefix);
}
});
return (String[]) result.toArray(new String[result.size()]);
}
}

View File

@ -0,0 +1,25 @@
package filter.hmimpl;
import java.util.ArrayList;
import java.util.List;
import filter.IFilter;
public class HMFilter implements IFilter {
public String[] filter(String[] values, String prefix) {
if(values == null) {
return null;
}
if(prefix == null) {
return values;
}
List result = new ArrayList();
for (int i = 0; i < values.length; i++) {
String string = values[i];
if(string != null && string.startsWith(prefix)) {
result.add(string);
}
}
return (String[]) result.toArray(new String[result.size()]);
}
}

View File

@ -0,0 +1,53 @@
package filter;
import junit.framework.TestCase;
public abstract class AbstractTestFilter extends TestCase {
public void testFilterNull() {
Exception err = null;
try {
getIFilter().filter(null, null);
} catch (NullPointerException npe) {
err = npe;
}
assertNull(err);
}
/**
* @return
*/
public abstract IFilter getIFilter() ;
public void testFilterNullValues() {
Exception err = null;
try {
getIFilter().filter(null, "test");
} catch (NullPointerException npe) {
err = npe;
}
assertNull(err);
}
public void testFilterNullPrefix() {
Exception err = null;
try {
getIFilter().filter(new String[]{"test"}, null);
} catch (NullPointerException npe) {
err = npe;
}
assertNull(err);
}
public void testFilter() {
String[] result = getIFilter().filter(new String[]{"test", "nogood", "mustbe filtered"}, "t");
assertNotNull(result);
assertEquals(result.length, 1);
}
public void testFilterWithNullValues() {
String[] result = getIFilter().filter(new String[]{"test", null, "mustbe filtered"}, "t");
assertNotNull(result);
assertEquals(result.length, 1);
}
}

View File

@ -0,0 +1,10 @@
package filter.ccimpl;
import filter.AbstractTestFilter;
import filter.IFilter;
public class CCFilterTest extends AbstractTestFilter {
public IFilter getIFilter() {
return new CCFilter();
}
}

View File

@ -0,0 +1,10 @@
package filter.hmimpl;
import filter.AbstractTestFilter;
import filter.IFilter;
public class HMFilterTest extends AbstractTestFilter {
public IFilter getIFilter() {
return new HMFilter();
}
}

View File

@ -0,0 +1,75 @@
<project name="myapp" default="run-cc" xmlns:ivy="antlib:fr.jayasoft.ivy.ant">
<!-- some variables used -->
<property name="lib.dir" value="lib" />
<property name="build.dir" value="build" />
<property name="src.dir" value="src" />
<!-- paths used for compilation and run -->
<path id="lib.path.id">
<fileset dir="${lib.dir}/build" />
</path>
<path id="run.hm.path.id">
<path location="${build.dir}" />
<fileset dir="${lib.dir}/noexternaljar" />
</path>
<path id="run.cc.path.id">
<path location="${build.dir}" />
<fileset dir="${lib.dir}/withexternaljar" />
</path>
<!-- =================================
target: resolve
================================= -->
<target name="resolve" description="--> retreive dependencies with ivy">
<ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact].[ext]"/>
</target>
<!-- =================================
target: report
================================= -->
<target name="report" depends="resolve" description="--> generates a report of dependencies">
<ivy:report todir="${build.dir}"/>
</target>
<!-- =================================
target: build
================================= -->
<target name="build" depends="resolve" description="--> compile the project">
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.path.id" />
</target>
<!-- =================================
target: run with ome made implementation
================================= -->
<target name="run-hm" depends="build" description="--> run the project with ome made implementation">
<java classpathref="run.hm.path.id" classname="myapp.Main" fork="true"/>
</target>
<!-- =================================
target: run with ext lib implementation
================================= -->
<target name="run-cc" depends="build" description="--> run the project with ext lib implementation">
<java classpathref="run.cc.path.id" classname="myapp.Main" fork="true"/>
</target>
<!-- =================================
target: clean
================================= -->
<target name="clean" description="--> clean the project">
<delete includeemptydirs="true">
<fileset dir="${basedir}">
<exclude name="src/**" />
<exclude name="build.xml" />
<exclude name="ivy.xml" />
<exclude name=".*" />
</fileset>
</delete>
</target>
<!-- =================================
target: clean-cache
================================= -->
<target name="clean-cache" description="--> clean the ivy cache">
<delete dir="${user.home}/.ivy/cache"/>
</target>
</project>

View File

@ -0,0 +1,13 @@
<ivy-module version="1.0">
<info organisation="jayasoft" module="myapp"/>
<configurations>
<conf name="build" visibility="private" description="compilation only need api jar" />
<conf name="noexternaljar" description="use only company jar" />
<conf name="withexternaljar" description="use company jar and third party jars" />
</configurations>
<dependencies>
<dependency org="jayasoft" name="filter-framework" rev="latest.integration" conf="build->api; noexternaljar->homemade-impl; withexternaljar->cc-impl"/>
</dependencies>
</ivy-module>

View File

@ -0,0 +1,16 @@
package myapp;
import java.util.Arrays;
import filter.FilterProvider;
import filter.IFilter;
public class Main {
public static void main(String[] args) {
String toFilter[] = new String[]{"one", "two", "tree", "four"};
IFilter filter = FilterProvider.getFilter();
System.out.println("Filtering with:"+filter.getClass());
String filtered[] = filter.filter(toFilter, "t");
System.out.println("Result :"+Arrays.asList(filtered));
}
}