寻找Webstart Maven插件示例应用程序

时间:2011-03-04 15:14:51

标签: maven-2 java-web-start sample maven-webstart-plugin

我正在寻找使用Webstart Maven插件的完整应用程序的源代码。

有什么想法吗?

2 个答案:

答案 0 :(得分:30)

我在涉及嵌入式tomcat服务器的概念中尝试了webstart插件。插件绑定到包阶段并需要很长时间才能执行,我建议从命令行手动调用它。它在目标目录中生成一个zip文件,其中包含jnlp文件和所有依赖项。然后可以将此文件引出并放在Web服务器上。 pom中的url应指向服务器上的此路径。启动时,应用程序在localhost端口8080上运行一个tomcat服务器,其中只有一个简单的servlet,它只是将请求的路径作为字符串返回。

请告诉我这是否适合您。

这是项目的主要内容,插件配置主要是从文档here中复制而来的

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.jhorstmann</groupId>
    <artifactId>EmbeddedTomcatWebstart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>EmbeddedTomcatWebstart</name>
    <url>http://localhost/jnlp/</url>
    <organization>
        <name>Organisation</name>
    </organization>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <tomcat.version>7.0.6</tomcat.version>
    </properties>
    <repositories>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>http://oss.sonatype.org/content/repositories/releases/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>net.jhorstmann.embeddedtomcat7.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo.webstart</groupId>
                <artifactId>webstart-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- bind to phase, I prefer to call it manualls -->
                        <phase>package</phase>
                        <goals>
                            <goal>jnlp-inline</goal> <!-- use jnlp, jnlp-inline or jnlp-single as appropriate -->
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--outputDirectory></outputDirectory--> <!-- not required?? -->

                    <!-- Set to true to exclude all transitive dependencies. Default is false. -->
                    <excludeTransitive>false</excludeTransitive>

                    <!-- The path where the libraries are stored within the jnlp structure. not required. by default the libraries are within the working directory -->
                    <libPath>lib</libPath>
                    <outputJarVersions>true</outputJarVersions>
                    <!-- [optional] transitive dependencies filter - if omitted, all transitive dependencies are included -->
                    <dependencies>
                        <!-- Note that only groupId and artifactId must be specified here. because of a limitation of the Include/ExcludesArtifactFilter -->
                        <!--
                        <includes>
                            <include>commons-logging:commons-logging</include>
                            <include>commons-cli:commons-cli</include>
                        </includes>
                        -->
                        <!--
                        <excludes>
                            <exclude></exclude>
                        <excludes>
                        -->
                    </dependencies>

                    <!--
                    <resourcesDirectory>${project.basedir}/src/main/jnlp/resources</resourcesDirectory>
                    -->
                    <!-- default value -->

                    <!-- JNLP generation -->
                    <jnlp>
                        <!-- default values -->
                        <!--inputTemplateResourcePath>${project.basedir}</inputTemplateResourcePath-->
                        <!--inputTemplate>src/main/jnlp/template.vm</inputTemplate--> <!-- relative to inputTemplateResourcePath -->
                        <outputFile>app.jnlp</outputFile> <!-- defaults to launch.jnlp -->

                        <!-- used to automatically identify the jar containing the main class. -->
                        <!-- this is perhaps going to change -->
                        <mainClass>net.jhorstmann.embeddedtomcat7.App</mainClass>
                    </jnlp>


                    <!-- SIGNING -->
                    <!-- defining this will automatically sign the jar and its dependencies, if necessary -->
                    <sign>
                        <keystore>${basedir}/keystore</keystore>
                        <keypass>password</keypass>  <!-- we need to override passwords easily from the command line. ${keypass} -->
                        <storepass>password</storepass> <!-- ${storepass} -->
                        <!--storetype>fillme</storetype-->
                        <alias>EmbeddedTomcatWebstart</alias>

                        <!--validity>fillme</validity-->

                        <!-- only required for generating the keystore -->
                        <dnameCn>EmbeddedTomcatWebstart</dnameCn>
                        <dnameOu>Organisation Unit</dnameOu>
                        <dnameO>Organisation</dnameO>
                        <dnameL>Location</dnameL>
                        <dnameSt>State</dnameSt>
                        <dnameC>Country</dnameC>

                        <verify>true</verify> <!-- verify that the signing operation succeeded -->

                        <!-- KEYSTORE MANAGEMENT -->
                        <keystoreConfig>
                            <delete>true</delete> <!-- delete the keystore -->
                            <gen>true</gen>       <!-- optional shortcut to generate the store. -->
                        </keystoreConfig>
                    </sign>

                    <!-- BUILDING PROCESS -->

                    <pack200>true</pack200>
                    <gzip>true</gzip> <!-- default force when pack200 false, true when pack200 selected ?? -->

                    <!-- causes a version attribute to be output in each jar resource element, optional, default is false -->
                    <outputJarVersions>false</outputJarVersions>

                    <!--install>false</install--> <!-- not yet supported -->
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-coyote</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>
</project>

这是放置在src/main/jnlp/template.vm的jnlp文件的自定义模板,我不记得为什么我需要这个:

<?xml version="1.0" encoding="utf-8"?>
<jnlp
    spec="$jnlpspec"
    codebase="$project.Url"
    href="$outputFile">
  <information>
    <title>$project.Name</title>
    <vendor>$project.Organization.Name</vendor>
    <homepage href="$project.Url"/>
    <description>$project.Description</description>
#if($offlineAllowed)
    <offline-allowed/>
#end
  </information>
#if($allPermissions)
  <security>
     <all-permissions/>
  </security>
#end
  <resources>
    <j2se version="$j2seVersion"/>
     $dependencies
  </resources>
  <application-desc main-class="$mainClass"/>
</jnlp>

这是src/main/java/net/jhorstmann/embeddedtomcat7/App.java

的主要课程
package net.jhorstmann.embeddedtomcat7;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

public class App {

    public static void main(String[] args) throws LifecycleException, ServletException, IOException {
        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
        File webappDir = new File(tmpDir, "embeddedtomcat7");
        webappDir.mkdir();

        final Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.setBaseDir(tmpDir.getAbsolutePath());
        tomcat.getConnector().setURIEncoding("UTF-8");

        String contextPath = "/";

        Context context = tomcat.addContext(contextPath, webappDir.getAbsolutePath());
        Wrapper wrapper = tomcat.addServlet(contextPath, "Test", new TestServlet());
        //Wrapper wrapper = tomcat.addServlet(contextPath, "Async", new AsyncServlet());
        //wrapper.setAsyncSupported(true);

        wrapper.addMapping("/*");

        tomcat.start();

        tomcat.getServer().await();
    }
}

最后是src/main/java/net/jhorstmann/embeddedtomcat7/TestServlet.java

的servlet
package net.jhorstmann.embeddedtomcat7;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<h1>" + req.getPathInfo() + "</h1>");
        writer.close();
    }
}

答案 1 :(得分:1)

关于这个插件的一些注释(它的文档非常糟糕):

除非您指定,否则不会使用template.vm文件,例如

<templateFilename>roll-gen-template.vm</templateFilename>

创造战争:

如果你想使用JnlpDownloadServlet(标准的java提供)来提供文件而不是上面的代码(并让插件生成一个工作的version.xml文件供它使用,等等),基本上你需要创建一个类型为war的新项目并定位jnlp-download-servlet goal(它似乎不支持使用当前pom项目中的类创建战争)然后,而不是单个<jnlp>部分,将改为使用<jnlpFiles>部分,它可以列出多个jar依赖项。您可能还需要修改web.xml文件。 http://www.mojohaus.org/webstart/webstart-maven-plugin/jnlp-mojos-overview.html有一个示例pom for

如果您有其他评论,请随时编辑,这是一个社区维基。