1.调试工程的时候,有时候要追踪源码文件
使用maven命令:mvn dependency:sources 下载依赖包的源代码。
或者
mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true
或者
eclipse窗口->首选项->maven->勾选download artifact sources
还有一种应该也行:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin>网站原文
Artifacts with sources deployed can be attached to Eclipse libraries using downloadSources. Javadocs may be attached using downloadJavadocs The sources and javadocs of the librariesmust exist in the repository so that the plugin can download it and attach it.
2.编译打jar包时要把源码也打包增加如下配置
<build>
<plugins><plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.列出jar包依赖关系
mvn dependency:tree > dep.log
4.解决资源文件编码问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
5.拷贝依赖的jar到lib目录下
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--这个地方应该指定你实际Main方法所在的类-->
<mainClass>com.test.MonitorMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 拷贝依赖的jar包到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
6.根据不同环境采用不同设置,通过<profiles>实现,<profiles>配置在<build>外
<profiles>
<profile>
<id>develop</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.url>jdbc:oracle:thin:@127.0.0.1:1521:ORCL</jdbc.url>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<jdbc.url>jdbc:oracle:thin:@228.7.75.102:1521:ORCL</jdbc.url>
</properties>
</profile>
</profiles>
同时配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<!--针对工程src/main/resources下的配置文件采用<profiles>-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
同时配置src/main/resources/db.properties
jdbc.url=${jdbc.url}
编译时制定参数:
测试环境直接 mvn clean package -Dmaven.test.skip=true 因为<activation>中设置默认采用测试环境配置
正式环境 mvn clean package -Dmaven.test.skip=true -Pproduct
7.mavn 制定编译器版本和字符编码
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
或者
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<!-- 设置源文件编码方式 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>