Jetty1組み込みスタートアップ

転載:http://www.jfinal.com/share/433


私が言いたいのは、私が言った解決策はこの記事http://www.jfinal.com/share/422とは何の関係もないということですが、この記事を読んだ後、私の解決策を共有してください。以前、桟橋を使用して自分のプロジェクトにパッケージ化してデプロイしたかったのです。図は便利です。この方法では、別の参照が提供されます。



この方法は、この資料に記載の方法とは少し異なり、ここでパッケージ化された構造は、独立したjarファイルのパッケージではありません。


次のようにパッケージが完了した後の構造がある:


/ -
   / confに
   / binに
   / libに
confに置き、構成ファイル情報、およびbinはスタートアップを格納しますスクリプトlibは、プロジェクト自体を含むjarパッケージの依存関係を格納します。


特定のメソッド:


pom.xml


<?xml version = "1.0" encoding = "UTF-8"?> <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 ">
 
  < groupId > com.example </ groupId>
  <artifactId> test </ artifactId>
  <version> 1.0 </バージョン>
  <packaging> jar </ packages>
  <name>テスト</ name>
  <description> test </ description>
 
  <properties>
    <project.build.sourceEncoding> UTF-8 </ project.build.sourceEncoding>
    <maven.compiler.encoding> UTF-8 </ maven.compiler.encoding>
    <pro- basedir> $ {project.basedir} </ pro-basedir>
    <project-charset> UTF-8 </ project-charset>
    <version-java> 1.8 </ version-java>
 
    <version-jfinal> 3.1 </ version- jfinal>
    <version-jetty> 9.4.6.v20170531 </ version-jetty>
  </ properties>
 
  <dependencies>
 
 
    <dependency>
      <groupId> org.eclipse.jetty </ groupId>
      <artifactId> jetty-server </ artifactId>
      <version> $ {version-jetty} </ version>
    </ dependency>
 
    <dependency>
      <groupId> org.eclipse.jetty </ groupId>
      <artifactId> jetty-webapp </ artifactId>
      <version> $ {version-jetty} </ version>
    </ dependency>
 
    <dependency>
      <groupId> com。 jfinal </ groupId>
      <artifactId> jfinal </ artifactId>
      <version> $ {version-jfinal} </ version>
      <exclusions>
        <exclusion>
          <groupId> cglib </ groupId>
          <artifactId> cglib-nodep </ artifactId>
        </排除>
      </排除>
    </依存性>
 
  </依存関係>
 
  <ビルド>
 
    <リソース>
      <リソース>
        <directory> $ {basedir} / src / main / resources </ directory>
        <filtering> true </ filtering>
      </ resource>
    </ resources>
    <testResources>
      <testResource>
        <directory> $ {basedir} / src / main / resources </ directory>
      </ testResource>
    </ testResources>
 
    <plugins>
 
      <plugin>
        <groupId> org.apache.maven.plugins </ groupId>
        <artifactId> maven-compiler-plugin </ artifactId>
        <configuration>
          < source> $ {version-java} </ source>
          <target> $ {version-java} </ target>
        </ configuration>
      </ plugin>
 
      <plugin>
        <groupId> org.apache.maven.plugins </ groupId>
        <artifactId> maven-source-plugin </ artifactId>
        <configuration>
          <includePom> true </ includePom>
        </ configuration>
        <executions>
          <execution>
            <id> attach-sources </ id>
            <phase> verify </ phase>
            <goals>
              <goal> jar-no-fork </ goal>
            </ goals>
          </ execution>
        </ executions>
      </ plugin>
 
      <プラグイン>
        <groupId> org.apache.maven.plugins </ groupId>
        <artifactId> maven-jar-plugin </ artifactId>
        <configuration>
          <includes>
            <!-打jar包時間、只打包クラス文件->
            <include> **/*。class</include>
          </ include >
          <finalName> $ {project.artifactId}-$ {project.version} </ finalName>
        </ configuration>
      </ plugin>
 
      <plugin>
        <groupId> org.apache.maven.plugins </ groupId>
        <artifactId> maven -assembly-plugin </ artifactId>
        <executions>
          <execution>
            <id> create-archive </ id>
            <フェーズ>パッケージ</フェーズ>
            <目標>
              <goal> single </ goal>
            </ goals>
            <configuration>
              <tarLongFileMode> gnu </ tarLongFileMode>
              <appendAssemblyId> false </ appendAssemblyId>
              <attach> false </ attach>
              <finalName> $ {project.name} </ finalName>
              <appendAssemblyId> false </ appendAssemblyId>
              <descriptors>
                <descriptor> src / main / assembly / pkg.xml </ descriptionor>
              </ descriptionors>
              <outputDirectory> $ {project.build.directory} / dist / </ outputDirectory >>
            </ configuration>
          </ execution>
        </ executions>
      </ plugin>
 
      <plugin>
        <groupId> org.apache.maven.plugins </ groupId>
        <artifactId> maven-resources-plugin </ artifactId>
        <dependencies>
          <dependency>
            <groupId> org.apache.maven.shared < / groupId>
            <artifactId> maven-filtering </ artifactId>
            <version> 1.3 </ version>
          </ dependency>
        </ dependencies>
      </ plugin>
 
    </ plugins>
 
  </ build>
 </ project>
はい、使用されます依存パッケージを移動するためのアセンブリプラグイン。アセンブリを追加し、src / mainディレクトリにpkg.xmlファイルを作成します。


pkg.xml


<?xml version = "1.0" encoding = "UTF-8"?> <アセンブリ>
 
  <id> dist </ id>
  <formats>
    <format> dir </ format>
    <format> tar.gz </ format>
  </ format >
  <baseDirectory> $ {project.name} </ baseDirectory>
 
  <includeBaseDirectory> true </ includeBaseDirectory>
 
  <fileSets>
    <fileSet>
      <directory> $ {project.basedir} </ directory>
      <outputDirectory> / </ outputDirectory>
      <includes>
        <include> README.md </ include>
      </ include >
    </ fileSet>
    <fileSet>
      <ディレクトリ> $ {プロジェクト。 basedir} / src / main / resources </ directory>
      <includes>
        <include> * </ include>
      </
      include > <outputDirectory> / conf </ outputDirectory>
      <filtered> true </ filtered>
    </ fileSet>
    <fileSet>
      <directory> $ {project.basedir} / src / main / webapp </ directory>
      <outputDirectory> / conf / webapp </ outputDirectory>
    </ fileSet>
    <fileSet>
      <directory> $ {project.basedir} / src / main / bin </ directory>
      <includes>
        <include> * </ include>
      </ include >
      < outputDirectory> / bin </ outputDirectory>
      <fileMode> 0755 </ fileMode>
    </ fileSet>
    <fileSet>
      <directory> $ {project.basedir} / target </ directory>
/ **  * 2017/7/25にiaceobによって作成されました。  * /






 















 



public class LightSowBoot {
 
    private void init()throws LightSowException {
        
        Boolean debug = PropKit.getBoolean( "test.debug");
 
        int model = ConfKit.getInt( "test.model"、0);
        if(model == 0)
            throw new RuntimeException( "Unknown Operating mode");
 
    }
 
    private String findWebdir(){
        //デバッグモードはwebdirアドレスを自動的に検出せず、構成ファイルに直接書き込むだけです
        if(PropKit.getBoolean( "test.debug"))
            return PropKit.get( "test.webdir.debug");
 
        //通常モードでは、環境変数を使用して/ webappという名前のアドレスを検索し、web.xmlファイルをここに配置します
        try {
            Enumeration <URL> urls = Thread。 currentThread()。getContextClassLoader()。 getResources( "");
            while(urls.hasMoreElements()){
                URL url = urls.nextElement();
                if(!url.getPath()。endsWith( "/ webapp /"))
                    続行;
 
                ファイルfile = new File(url.getPath());
                if(!file.isDirectory())
                    続行;
 
                url.getPath();を返します。
            }
            nullを返します。
        } catch(IOException e){
            throw new RuntimeException( "Can not find webapp。");
        }
    }
 
    public void boot(){
        PropKit.use( "cfg.properties");
        {を試してください
            this.init();
        } catch(RuntimeException e){
            log.error(e.getMessage()、e);
        }
 
        int port = PropKit.getInt( "test.srv.port");
        {
            サーバーサーバー=新しいサーバー(ポート);を試してください
            WebAppContext ctx = new WebAppContext();
 
            文字列webdir = findWebdir();
 
            ctx.setResourceBase(webdir);
            ctx.setDescriptor(String.format( "%s / WEB-INF / web.xml"、webdir));
            ctx.setContextPath( "/");
            ctx.setParentLoaderPriority(true);
            ctx.setConfigurations(new Configuration [] {
                new WebXmlConfiguration()、
                new WebInfConfiguration()、
                // new TagLibConfiguration()、
                new MetaInfConfiguration()、
                new FragmentConfiguration()、
                new JettyWebXmlConfiguration()
            });
 
            log.info( "サーバーリッスン{}"、ポート);
 
            server.setHandler(ctx);
            server.start();
            server.join();
        } catch(InterruptedException e){
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
 
}これで
終わりです。web.xmlのコンテンツを投稿する必要はありません。


最後に、パッケージ化と解凍後のディレクトリ構造は 次のようになります



| -conf
| | -webapp
| | -web.xml
| -bin
| | -boot.sh
| -lib
    | -a.jar
    | -b.jar
Javaプログラムを開始するための起動スクリプト


は、ここで確認できます。シェルスクリプト は、桟橋起動モードの使用について、実際に続行できます。 Webを作成する必要はありません。.xml構成ファイルをカプセル化する必要があるのは1回だけで、JfinalConfigを書き直して起動メソッドを提供し、Configを継承した後にmainメソッド呼び出しを作成します。

おすすめ

転載: blog.csdn.net/gredn/article/details/78199630