maven打包源碼
A. 如何配置pom.xml用maven打包java工程
1,生成3個目錄/lib,/conf,/bin目錄
2,把所有的jar目錄編譯、拷貝到/lib目錄(包括maven的jar包和lib目錄下的jar,以及編譯的jar包)
3,把所有的啟動腳本從工程根目錄拷貝到/bin目錄
4,把所有的配置文件從src/main/resources拷貝到/conf
pom.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test.common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test.common</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 這里省略n行 -->
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<!-- 把src/main/resources目錄下所有的文件拷貝到conf目錄中 -->
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/conf</targetPath>
</resource>
<!-- 把lib目錄下所有的文件拷貝到lib目錄中
(可能有些jar包沒有辦法在maven中找到,需要放在lib目錄中) -->
<resource>
<directory>lib</directory>
<targetPath>${project.build.directory}/lib</targetPath>
</resource>
<!-- 把放在根目錄下的腳本文件.sh,.bat拷貝到bin目錄中 -->
<resource>
<directory>.</directory>
<includes>
<include>**/*.sh</include>
<include>**/*.bat</include>
</includes>
<targetPath>${project.build.directory}/bin</targetPath>
</resource>
</resources>
<plugins>
<!-- 用於編譯的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<fork>true</fork>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<!-- 如果配置了JAVA_HOME,下面應該可以不用配 -->
<executable>C:\Program Files (x86)\Java\jdk1.8.0_91\bin\javac.exe</executable>
</configuration>
</plugin>
<!-- 用於生成jar包的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- 把生成的jar包放在lib目錄下(和其他所有jar包一起) -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<excludes>
<!-- 排除掉一些文件,不要放到jar包中,
這里是為了排除掉src/main/resources中的文件(它們應該放到conf目錄)
這里只能指定要排除的目標文件,而不能指定源文件,雖然不夠完美,但是基本能達到目的。 -->
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<!-- 用於拷貝maven依賴的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>-dependencies</id>
<phase>package</phase>
<goals>
<goal>-dependencies</goal>
</goals>
<configuration>
<!-- 把依賴的所有maven jar包拷貝到lib目錄中(這樣所有的jar包都在lib目錄中) -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 用於拷貝resource的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 配置生成源代碼jar的plugin -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<attach>true</attach>
<encoding>UTF-8</encoding>
<!-- 配置源代碼jar文件的存放路徑,和其他jar文件一起放在lib目錄 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然後執行maven package打包。如果是eclipse,在Project Explorer-> Run As –> Maven Build… –>在Goals中輸入package,點Run進行編譯。
B. 如何用maven將配置文件打在jar包外
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--打包源碼一起發布到maven倉庫中-->
<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>
<!--生成可執行JAR包命令maven-jar-plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>***MainApplication***</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!--拷貝依賴的jar包到lib目錄-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id></id>
<phase>package</phase>
<goals>
<goal>-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--生成可執行JAR包命令maven-jar-pluginend-->
<!--生成可執行JAR包命令maven-shade-plugin<plugin><groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId><version>1.2.1</version><executions>
<execution><phase>package</phase><goals><goal>shade</goal></goals><configuration>
<transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>***.Application***</mainClass></transformer>
</transformers></configuration></execution></executions></plugin>-->
</plugins>
</build>
可以參考這個配置,使用maven package將在target下面生成jar包,lib是依賴庫,配置文件放在和jar一個目錄
C. 怎麼發布一個pom項目且打包maven源碼,將源
在pom.xml中添加:
<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>
配置中指定了phase為compile,意思是在生命周期compile的時候就將源文件打包,即只要執行的mvn命令包括compile這一階段,就會將源代碼打包。同樣,phase還可以指定為package、install等等。
D. 如何用java的maven工具給一個項目的源代碼打包成jar(裡面放編譯好的class文件)
你這樣也太讓人為難了... 不可能直接給你一pom解決,這個沒按照約定,那就得配置,那maven學習成本有點高,勸你看下ant,編寫個build,1小時之內搞定。
E. maven中如何打包源代碼
在pom.xml中添加:
<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>
配置中指定了phase為compile,意思是在生命周期compile的時候就將源文件打包,即只要執行的mvn命令包括compile這一階段,就會將源代碼打包。同樣,phase還可以指定為package、install等等。
F. spring源碼不是maven打包的嗎
3.2之後改成gradle了,pom可以從這里查http://search.maven.org/
在advanced search里可以按GroupId,ArtifactId,Version查詢
G. 如何把github上下載下來的maven源代碼zip文件打包成可運行的jar文件
用IDEA克隆項目,再把maven與IDEA整合,如圖所示點這個:
再點綠色三角,就可以打包。
這項目真大,等我下好直接把jar包發給你。
H. maven打包時怎麼把所以的依賴一起打包
maven-jar-plugin 只能控制你的源碼如何打包,如果需要把依賴的 jar 包一起打包,需要使用 maven-assembly-plugin
I. 在Ubuntu中,用mvn打包hadoop源代碼是報錯,咋回事
將hadoop源碼解壓到一個目錄,注意目錄層次不要太深,否則可能無法解壓。
進入hadoop-maven-plugins文件夾,執行 mvn install
返回源碼根目錄,執行 mvn eclipse:eclipse –DskipTests
eclipse在任意目錄創建新的WorkSpace
eclipse設置Maven:window->preference->maven->{Installations...;user Settings:maven\conf\settings.xml}
eclipse:File->inport->Existing Projects into WorkSpace->Hadoop源碼根目錄
J. 如何用java的maven將一個下載了的源代碼打成jar包
在項目上面右鍵,有個maven菜單,裡面有個bulid還是package的。可以打包