maven打包之新手拙见

maven打包一直都困扰着我这种新手,现在我也有点明白这是一个什么东西了。把maven打包问题总结一下。

首先配置好各种东西,能运行打包之后再细细研究一下这个打包的配置文件。

pom.xml文件:

添加各种jar包引用不多讲了

下来是配置主类这个东西:pom.xml中添加<bulid>

内容如下:

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<version>2.4</version>
		<configuration>
			<archive>
				<manifest>
					<addClasspath>true</addClasspath>
					<classpathPrefix>lib/</classpathPrefix>
					<mainClass>com.sysware.HelloWorld</mainClass>
				</manifest>
			</archive>
		</configuration>

    <span style="white-space:pre">	</span><resources>
       <span style="white-space:pre">	</span> <resource>
            <directory>src/main/java</directory>
            <includes>
            <include>**/*.properties</include>    把配置文件等也打到包内
            <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources> 

			</plugin>

 这样就可以对简单工程进行打包,但是,如果有各种各样的文件存在,需要放到包内或者包外面,则需要自定义打包的情况,用assembly插件打包

根据需要,我们又要把一些文件打到包外, 放到和包同级的文件夹内,配置文件变为如下:

<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>demo</groupId>
  <artifactId>demo</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
  <plugins>
  		<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-assembly-plugin</artifactId>
		<version>2.3</version>
		<configuration>
			<appendAssemblyId>false</appendAssemblyId>
			<descriptorRefs>
				<descriptorRef>jar-with-dependencies</descriptorRef>
			</descriptorRefs>
			<archive>
				<manifest>
					<mainClass>demo.App</mainClass>
				</manifest>
			</archive>
			<descriptors>
                   <descriptor>src/main/assemble/package.xml</descriptor>   多了这个描述标签,需要新建src/main/assemble/package.xml文件

              </descriptors>                   这个文件就是assembly打包要描述的,配置一下你需要另外打包的文件夹,各种
    <span style="white-space:pre">
        </span>                                         文件等,我新加文件夹conf里面放conf.xml文件
	</configuration>
		<executions>
			<execution>
				<id>make-assembly</id>
				<phase>package</phase>
			<goals>
				<goal>assembly</goal>
			</goals>
		      </execution>
		</executions>
	</plugin>
  </plugins>
  	<resources>      这一段是给jar包里面把配置文件加入
       	 <resource>
            <directory>src/main/java</directory>
            <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
  </build>
</project>

package.xml配置如下:

<assembly 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/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/java</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>conf</directory>    需要打包的文件夹
            <outputDirectory>config</outputDirectory>   输出的文件夹
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>      这个放置依赖jar包,以后再研究
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

打包后,去仓库看看,多了一个文件夹,里面放置着另外打包出来的东西

如果程序中需要读取xml文件或配置文件,不放在jar包内,就把配置文件这样打包出来,程序内部的路径要用classloader或者System.setProperties();

具体上一个博文中都介绍了,两个一起用,解决打自定义包和外部配置文件引用问题

maven打包之新手拙见,布布扣,bubuko.com

时间: 2024-08-24 22:42:50

maven打包之新手拙见的相关文章

使用maven打包项目并上传到Linux服务器

maven打包: 项目右键Run as-->Maven build...-->  出来下面的界面,注意红色部分的填写,Goals填写package表示打包,下面的Skip Tests表示打包时不执行单元测试. 点击Run 就打包成功了,jar包在空间下/项目名/target下.没有打包配置文件. 将打包好的jar上传到Linux服务器: 首先通过cd 进入到需要上传的目录下,然后是输入rz命令回车,弹出窗口选择要上传的包,上传,最后通过ls命令查看有没有上传成功. 前两天通过这个方法上传成功了

maven 打包 java中的配置文件

转发地址 http://www.cnblogs.com/liuzy2014/p/5981824.html 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里. 有时候,比如mybatis的mapper.xml文件,我们习惯把它和Mapper.java放一起,都在src/main/java下面,这样利用maven打包时,就需要修改pom.xml

利用Maven打包时,如何包含更多的资源文件

首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里. 有时候,比如mybatis的mapper.xml文件,我们习惯把它和Mapper.java放一起,都在src/main/java下面,这样利用maven打包时,就需要修改pom.xml文件,来把mapper.xml文件一起打包进jar或者war

hive udaf 用maven打包运行create temporary function 时报错

用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com.leaf.data.Maximum"; java.lang.SecurityException: Invalid signature file digest for Manifest main attributes at sun.security.util.SignatureFileVerifier

基于spring打包 maven 打包jar项目

J2EE项目开发 一般使用tomcat等servlet容器,有些自动化的任务却不需要放在servlet容器中, 把任务打包成jar会更方便部署 使用Maven打包Spring的jar包

Idea开发环境中搭建Maven并且使用Maven打包部署程序

1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME的环境变量,然后将该变量添加到Path中 备注:必须要有JAVA_HOME的M2_HOME环境变量,不然Maven会提示错误.配置环境变量如图所示: c.如果想要修改Maven的本地仓库位置,则可以直接在Maven的安装目录下找到conf文件下的setting配置文件中,设置localRepository为本地仓库位置 <localRepository>E:\java

Vcenter数据采集之maven打包可执行jar血历史

技术点:spring加载xsd过程: http://blog.csdn.net/bluishglc/article/details/7596118 案发背景: 1.一个使用maven管理的普通项目(vcenter-collector :关于Vcenter数据采集) 2.项目主要技术: java + mybatis(基于接口形式)+其他 3.项目中有一个非maven仓库的jar包:vijava(做vcenter数据采集的),在项目中以system方式引用 (该jar放在${project.base

maven打包步骤

maven打包1:先在pom文件中添加下面配置<build>        <plugins>            <!-- compiler插件, 设定JDK版本 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plug

maven打包异常:软件包com.sun.org.apache.xml.internal.security.utils.Base64 不存在

maven打包异常:软件包com.sun.org.apache.xml.internal.security.utils.Base64 不存在 将jre/lib/rt.jar添加到maven的compiler里面  编译正常... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <versio