Eclipse builds Java SE Engineering configuration log4j through MAVEN, packages into zip, separates jar packages from configuration files, and launches Java programs with bat and SH files

Source: Internet
Author: User
Tags log4j java se

I. New MAVEN Java project

1.eclipse file-new-other, select Maven Project

2. Select Use default Workspace location, then next

3. Select Maven-archetype-quitstart, then next

4. Fill in the project information and finish

6. Create a good engineering structure

7. Delete the Java class automatically generated under test and Java, right-Build-path Select the JDK version, the whole project is built.

Two. Configure log4j

1. Adjust the project catalog (this step is actually prepared for the following MAVEN package)

2. Importing Log4j,jar via Maven

Add the following statement to the <dependencies> in Pom.xml

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

3. Under Src/conf, create a new file log4j.properties, which reads as follows:

 # # # #log4j. Rootlogger = debug,stdout,d,e### Output information to the console # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = [%-5p]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n### output debug levels above the log to E://logs/log.log # # #log4j. APPENDER.D = Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file = E://logs/log.loglog4j.appender.d.append = Truelog4j.appender.d.threshold = DEBUG Log4j.appender.d.layout = Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m %n### output error level above the log to E://logs/error.log # # #log4j. APPENDER.E = Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file =e://logs/error.log log4j.appender.E.Append = Truelog4j.appender.e.threshold = ERROR Log4j.appender.e.layout = Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:SS} [%t:%r]-[%p]%m%n 

4. Create a new log test class with the following contents:

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  //Record debug level information          logger.debug ("This is D Ebug message. ");          Log information for info level          Logger.info ("This is info message.");          Record the error level information          logger.error ("This is the error message.");}

5. The results are as follows:

5.1 Typing the following information in the console

5.2 Two files appear under the Logs folder on E-drive

The contents were as follows:

Three. Configure Maven-assembly-plugin and package

1. Change the contents of the Pon.xml to the following (the document structure of the project is above)

<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 Log--><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>&Lt;/configuration></plugin></plugins></build></project> 

The package.xml content in the

2.src/main/assemble directory is:

<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> <includebasedirect ory>true</includebasedirectory> <fileSets> <fileSet> <directory>src/main/b in</directory> <outputDirectory>bin</outputDirectory> </fileSet> <file Set> <directory>src/main/conf</directory> <outputdirectory>conf</outputdirec            tory> </fileSet> <fileSet> <directory>src/main/logs</directory> <outputDirectory>logs</outputDirectory> </fileSet> <fileSet> <dir Ectory>src/main/work</directory> <outputDirectory>work</outputDirectory> </fileSet> </fileSets> <de pendencysets> <dependencySet> <outputDirectory>lib</outputDirectory> &lt ;scope>runtime</scope> </dependencySet> </dependencySets></assembly>

3. Under Src/main/bin, create a new file Start.bat and start.sh two files for Windows and Linux to start Java programs

The contents of Start.bat are as follows:

@echo OffSET base=%~dp0set main_class= "Com.zhuzhixi.zhuzhixiDataMaker.Test.LogTest" SET conf=%base%. \confset libs=%base%. \libset Class_path=%conf%;%libs%\*;java-classpath%class_path%%main_class% @pause

  

The contents of start.sh are as follows:

#!/bin/bashscript_path=$ (CD "$ (dirname") "; pwd" lib_path=${script_path}/.. /libmain_class= "Com.zhuzhixi.zhuzhixiDataMaker.Test.LogTest" conf_path=${script_path}/. /etc/confclasspath= "${conf_path}" for jar in $lib _path/*.jardoif ["$CLASSPATH" = ""]; Thenclasspath= $jarelseCLASSPATH = $CLASSPATH: $jarfidoneexport classpath= $CLASSPATHecho "java ${main_class} \" [Email Protected]\ ""

4. Right-click Project-debug as-maven assembly:assembly

The output log is packaged successfully.

Four. Testing

1. Unzip the zip file, the folder structure is as follows, all files are present:

2. Enter the bin and run the Start.bat (the JRE environment needs to be installed in the running environment)

3. The results are as follows:

Under the Logs folder in E-drive

Log.log content is as follows:

Error.log content is as follows

Therefore, the Conf log4j.properties is correctly read.

Eclipse builds Java SE Engineering configuration log4j through MAVEN, packages into zip, separates jar packages from configuration files, and launches Java programs with bat and SH files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.