首页 > 代码库 > eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序

eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序

一.新建maven的java工程

1.eclipse里file-new-other,选择maven Project 

技术分享

2.选中 Use default Workspace location,然后 next

技术分享

3.选择maven-archetype-quitstart,然后next

技术分享

4.填入工程信息,然后finish

技术分享

6.新建好的工程结构如图

 技术分享

7.将test下和java下自动生成的java类删除,右键build-path选择好jdk版本,整个工程就建好了。

二.配置log4j

1.将工程目录调整成下图(这步其实是为下面maven打包做准备)

技术分享

 

2.通过maven导入log4j,jar

在pom.xml的<dependencies>里加入以下语句

    <dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.6.0</version>
</dependency>        

3.在src/conf下面新建文件log4j.properties,内容如下:

 ### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制台###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出debug级别以上的日志到E://logs/log.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出error级别以上的日志到E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

4.新建日志测试类,内容如下:

package com.zhuzhixi.zhuzhixiDataMaker.Test;

import org.apache.log4j.Logger;

public class LogTest {

	private static Logger logger = Logger.getLogger(LogTest.class);  
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		  // 记录debug级别的信息  
        logger.debug("This is debug message.");  
        // 记录info级别的信息  
        logger.info("This is info message.");  
        // 记录error级别的信息  
        logger.error("This is error message."); 
	}

}

5.结果如下:

5.1在控制台打出如下信息

技术分享

5.2在e盘的logs文件夹下出现两个文件

技术分享

 

内容分别如下:

技术分享

 

技术分享

三.配置maven-assembly-plugin并打包

1.在pon.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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zhuzhixi</groupId>
	<artifactId>zhuzhixiDataMaker</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<description></description>

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

	<dependencies>
		<!-- log4j日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.0</version>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<directory>src/main/conf</directory>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.7</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/assemble/package.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<verbose>true</verbose>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

2.src/main/assemble目录下的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>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/logs</directory>
            <outputDirectory>logs</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/work</directory>
            <outputDirectory>work</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

3.在src/main/bin下面新建文件start.bat和start.sh两个文件,用于windows和linux里启动java程序

start.bat的内容如下:

@echo off

SET base=%~dp0
SET main_class="com.zhuzhixi.zhuzhixiDataMaker.Test.LogTest"

SET conf=%base%..\conf
SET libs=%base%..\lib
SET class_path=%conf%;%libs%\*;
java -classpath %class_path% %main_class%
@pause

  

start.sh的内容如下:

#!/bin/bash


script_path=$(cd "$(dirname "$0")"; pwd)
lib_path=${script_path}/../lib
main_class="com.zhuzhixi.zhuzhixiDataMaker.Test.LogTest"
conf_path=${script_path}/../etc/conf

CLASSPATH="${conf_path}"
for jar in $lib_path/*.jar
do
	if [ "$CLASSPATH" = "" ] ; then
		CLASSPATH=$jar
	else
		CLASSPATH=$CLASSPATH:$jar
	fi
done

export CLASSPATH=$CLASSPATH
echo "java ${main_class} \"$@\""

4.右键工程-Debug As-maven assembly:assembly

技术分享

输出日志如下图则打包成功。

技术分享

四.测试

1.解压zip文件,文件夹结构如下,所有文件均存在:

技术分享

2.进入bin,运行start.bat(运行环境需要安装jre环境)

3.结果如下:

技术分享

在e盘的logs文件夹下

 log.log内容如下:

技术分享

error.log内容如下

技术分享

 因此,conf的log4j.properties正确被读取了。

 

eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序