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

14

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

有什么建议吗?


在 Sonatype 公共仓库的搜索结果中没有显示任何依赖它的内容。 :( - David J. Liszewski
2个回答

30

我尝试在嵌入式Tomcat服务器中使用Webstart插件进行概念验证。该插件绑定到打包阶段并需要很长时间才能执行,建议手动从命令行调用它。它生成一个zip文件,其中包含jnlp文件和所有依赖项,该文件可以解压并放置在web服务器上。 pom中的URL应指向服务器上的此路径。启动时,应用程序在本地主机端口8080上运行Tomcat服务器,并返回请求路径作为字符串的简单servlet。

如果对你有用,请告诉我。

以下是项目的pom.xml文件,插件配置大部分是从此处的文档中复制的。

<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();
    }
}

最后,一个servlet在src/main/java/net/jhorstmann/embeddedtomcat7/TestServlet.java

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();
    }
}

运行它的命令是什么? - jcalfee314
在上面的pom.xml文件中有两件事情需要注意:第一是<phase>package</phase>,第二是<goal>jnlp-inline</goal>。我曾经遇到过这个插件找不到我的mainClass的问题。我尝试将jnlp更改为jnlp-inline,但没有任何作用。这是因为jnlp不需要阶段,而jnlp-inline需要。 - Keenan

1
以下是关于这个插件的一些注释(它的文档很糟糕):
如果不指定,它不会使用template.vm文件。

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

创建一个war文件:
如果你想使用JnlpDownloadServlet(Java提供的标准服务)来提供文件,而不是使用上述代码(并让插件生成可用的version.xml文件等),基本上你需要创建一个新的war类型项目,并且目标是“jnlp-download-servlet goal”(似乎不支持使用当前pom项目中的类创建war文件),然后,你将有“<jnlpFiles>”部分代替单个“<jnlp>”部分,其中可以列出多个jar依赖项。你可能还需要修改你的web.xml文件。 http://www.mojohaus.org/webstart/webstart-maven-plugin/jnlp-mojos-overview.html有一个示例pom。
如果您有其他意见,请随时编辑此内容,这是一个社区wiki。

https://web.archive.org/web/20150118052707/http://mojo.codehaus.org/webstart/webstart-maven-plugin/jnlp-mojos-overview.html - andrej

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接