zoukankan      html  css  js  c++  java
  • maven创建项目,打包出可执行Jar

    官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式

    这个例子也不错 https://blog.csdn.net/u010010606/article/details/79758030

    想弄一个maven打出来可执行的jar

    mvn archetype:generate   创建一个mvn项目,输入一系列创建参数后,成功。

    mvn dependency:copy-dependencies -DoutputDirectory=target/lib 可以将依赖的包导出到某个目录

    然后做如下修改,加入依赖,并且为了将内容打包到执行的Jar,需要加入assembly,并制定打包后执行的主类

    <?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">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.test</groupId>
      <artifactId>netty</artifactId>
      <version>1.0</version>
    
      <name>netty</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.31.Final</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.7.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.20.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.test.netty.Server</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>assembly</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

    执行命令打包

    mvn assembly:assembly

    运行jar

    java -jar ***.jar

    定制打包描述文件

    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>assemble</id>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <phase>package</phase>
                            <configuration>
                                <appendAssemblyId>false</appendAssemblyId>
                                <attach>false</attach>
                                <descriptors>
                                    <descriptor>***/assembly1.xml</descriptor>
                                </descriptors>
                            </configuration>
                        </execution>
                        <execution>
                            <id>targz</id>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <appendAssemblyId>false</appendAssemblyId>
                                <attach>false</attach>
                                <descriptors>
                                    <descriptor>***/assembly2.xml</descriptor>
                                </descriptors>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

    assembly1.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
     
        <id>tgz</id>
        <!-- 应用名-run(压缩包解压后的目录名) -->
        <baseDirectory>/</baseDirectory>
        <formats>
            <format>dir</format>
        </formats>
        
        <fileSets>
            <fileSet>
                <directory>src/bin</directory>
                <outputDirectory>bin</outputDirectory>
                <fileMode>0755</fileMode>
                <directoryMode>0755</directoryMode>
            </fileSet>
            <fileSet>
                <directory>src/conf</directory>
                <outputDirectory>conf</outputDirectory>
            </fileSet>
        </fileSets>
        <dependencySets>
            <dependencySet>
                <useProjectArtifact>false</useProjectArtifact>
                <!-- 运行时依赖统一放在lib目录下 -->
                <outputDirectory>lib</outputDirectory>
                <scope>runtime</scope>
            </dependencySet>
        </dependencySets>
    </assembly>

    assembly2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
     
        <id>tgz</id>
        <!-- 应用名-run(压缩包解压后的目录名) -->
        <baseDirectory>quarkservice-run-${project.version}</baseDirectory>
        <formats>
            <format>tar.gz</format>
        </formats>
        
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/${project.build.finalName}</directory>
                <outputDirectory>/</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
  • 相关阅读:
    nodejs关于前后端图片上传的思路及实现代码
    vue项目better-scroll使用注意点
    nuxt.js的使用和开发,一款vue基于服务器SSR渲染工具
    vue-cli3.0中自定css、js和图片的打包路径
    Vue.js watch监视属性
    React Developers的10个超实用神奇工具
    Vue 3.0 体验 Vue Function API
    Dojo Store 概念详解
    React Native 实现城市选择组件
    java 环境变量 设置 问题
  • 原文地址:https://www.cnblogs.com/it-worker365/p/9929292.html
Copyright © 2011-2022 走看看