Version 4
    Introduction

    This shows a basic ant configuration for running Embedded AS. The current way this is accomplished is by including jboss-embedded-assembly-<version>-launcher.jar and the shrinkwrap libs (shrinkwrap-api-<version>.jar shrinkwrap-impl-base-<version>.jar shrinkwrap-spi-<version>.jar) in a test or runtime lib directory. All of your code dependencies are then included in a compile-time lib directory (shrinkwrap and jboss embedded libs are required there too). At runtime, *only* the two jboss-bootstrap-api jars must be in the classpath as well.

     

     

    <project name="HelloWorld" default="compile" basedir=".">
      <property name="proj.name" value="HelloWorld"/>
      <property name="proj.version" value="1.0"/>

     

      <property environment="env"/>
      <property name="src.java.dir" value="${basedir}/src"/>
      <property name="src.etc.dir" value="${basedir}/etc"/>
      <property name="compile.lib.dir" value="${basedir}/lib"/>
      <property name="test.lib.dir" value="${basedir}/testlib"/>
      <property name="build.dir" value="${basedir}/bin"/>
      <property name="jboss.home" value="${env.JBOSS_HOME}"/>

     

      <fileset id="jboss-bootstrap-apis" dir="${jboss.home}/lib">
        <include name="jboss-bootstrap-api.jar"/>
        <include name="jboss-bootstrap-api-as.jar"/>
      </fileset>

     

      <fileset id="testlibs" dir="${test.lib.dir}">
        <include name="**/*.jar"/>
      </fileset>

     

      <path id="compile.classpath">
        <fileset dir="${compile.lib.dir}">
          <include name="**/*.jar"/>
        </fileset>

     

        <fileset refid="testlibs"/>
      </path>

     

      <path id="runtime.classpath">
        <fileset refid="testlibs"/>
        <fileset refid="jboss-bootstrap-apis"/>
        <fileset dir="${build.dir}"/>
      </path>

     

      <patternset id="meta.files">
        <include name="**/*.xml"/>
        <include name="**/*.properties"/>
      </patternset>

     

      <target name="clean">
        <delete dir="${build.dir}"/>
        <mkdir dir="${build.dir}"/>
      </target>
     
      <target name="compile" depends="clean">
        <mkdir dir="${build.dir}"/>
        <javac
           srcdir="${src.java.dir}"
           destdir="${build.dir}"
           nowarn="on">
          <classpath refid="compile.classpath"/>
        </javac>
      </target>

     

      <target name="copymetafiles">
        <copy todir="${build.dir}">
          <fileset dir="${src.java.dir}">
        <patternset refid="meta.files"/>
          </fileset>
        </copy>
        <copy todir="${build.dir}">
          <fileset dir="${src.etc.dir}">
        <patternset refid="meta.files"/>
          </fileset>
        </copy>
      </target>

     

      <target name="run" depends="compile, copymetafiles" description="Build and run HelloWorld">
        <java fork="true"
          classname="hello.HelloWorld"
          classpathref="runtime.classpath">
          <classpath path="${build.dir}"/>
        </java>
      </target>
    </project>

     

    The contents of test.lib.dir in this case would be something like:

     

    jboss-embedded-assembly-1.0.0-SNAPSHOT-launcher.jar
    shrinkwrap-api-1.0.0-alpha-4.jar
    shrinkwrap-impl-base-1.0.0-alpha-4.jar
    shrinkwrap-spi-1.0.0-alpha-4.jar

     

    Example client code (incidentally taken from an early EJB example from Java Persistence with Hibernate):

     

    package hello;

     

    import javax.naming.InitialContext;
    import java.net.URL;
    import java.io.File;
    import java.net.MalformedURLException;

     

    import org.jboss.bootstrap.api.lifecycle.LifecycleState;
    import org.jboss.embedded.api.server.JBossASEmbeddedServer;
    import org.jboss.embedded.api.server.JBossASEmbeddedServerFactory;
    import org.jboss.shrinkwrap.api.Archives;
    import org.jboss.shrinkwrap.api.spec.JavaArchive;
    import org.jboss.shrinkwrap.impl.base.path.BasicPath;

     

    public class HelloWorld {
        private static JBossASEmbeddedServer server;
        private static InitialContext initialContext;

     

        public static void main(String[] args) throws Exception {
        server = JBossASEmbeddedServerFactory.createServer();
        System.out.println("Created: " + server);

        // Start the Server
        server.start();

     

        // Set Naming Context
        initialContext = new InitialContext();

     

        // Create archive
        JavaArchive archive = Archives.create("helloworld.jar", JavaArchive.class)
            .addClasses(MessageHandlerBean.class, MessageHandler.class, Message.class)
            .addManifestResource("META-INF/persistence.xml", new BasicPath("persistence.xml"));

     

        // Deploy
        System.out.println("deploying " + archive.toString(true));
        server.deploy(archive);

     

        // Test
        MessageHandler mh = (MessageHandler) initialContext.lookup(MessageHandlerBean.class.getSimpleName() + "/local");

     

        mh.saveMessages();
        mh.showMessages();

     

        // Shutdown if started
        if (server != null && server.getState().equals(LifecycleState.STARTED)) {
            // Shutdown
            System.out.println("Shutting down server: " + server);
            server.shutdown();
        }

     

        }

     

        private static URL getJBossHome()
        {
        try
            {
            return new File(System.getProperty("jboss.home")).toURI().toURL();
            }
        catch (final MalformedURLException murle)
            {
            throw new RuntimeException("Could not get JBOSS_HOME", murle);
            }
        }
    }