maven項目打包額外lib目錄

maven項目依賴了幾個額外的jar包一直都沒法打進最終jar,不知道哪裏出了問題.一直對這塊不甚清楚,就大概梳理一下java

默認打包方式:apache

  maven項目下,默認編譯目錄爲maven

src/main/java和src/test/java 

  和ui

src/main/resouces和src/test/resources

因此按照默認條件,其餘目錄的東西都不會被打進去,並且這些目錄底下的一些非符合條件文件也不會被打包,java目錄下只會打包.java文件,具體看target/classes目錄下的文件便可spa

除非特別指定,不然,打包時將會把target/classes目錄下的東西打包成jar插件

那麼問題來了,怎麼把想要加入打包的東西弄到目錄target/classes下呢debug

這個是由resource決定的,當須要將一些特殊文件打包時,須要額外配置resource標籤:code

  好比常常碰到的在src/main/java中寫的xml須要打包到jar,須要配置:orm

    <build>
       <!-- 資源目錄 -->    
        <resources>    
            <resource>    
                <!-- 設定主資源目錄  -->    
                <directory>src/main/java</directory>    
 
                <!-- maven default生命週期,process-resources階段執行maven-resources-plugin插件的resources目標處理主資源目下的資源文件時,只處理以下配置中包含的資源類型 -->     
                 <includes>
                      <include>**/*.xml</include>
                 </includes>  
                     
                <!-- maven default生命週期,process-resources階段執行maven-resources-plugin插件的resources目標處理主資源目下的資源文件時,不處理以下配置中包含的資源類型(剔除下以下配置中包含的資源類型)-->      
                <excludes>  
                    <exclude>**/*.yaml</exclude>  
                </excludes>  
 
                <!-- maven default生命週期,process-resources階段執行maven-resources-plugin插件的resources目標處理主資源目下的資源文件時,指定處理後的資源文件輸出目錄,默認是${build.outputDirectory}指定的目錄-->      
                <!--<targetPath>${build.outputDirectory}</targetPath> -->      
   
                <!-- maven default生命週期,process-resources階段執行maven-resources-plugin插件的resources目標處理主資源目下的資源文件時,是否對主資源目錄開啓資源過濾 -->    
                <filtering>true</filtering>     
            </resource>              
        </resources>     
  </build>

  因此,若是要加入jar包一樣須要加入resourcexml

<resource>
                <directory>lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>

把lib目錄下的全部jar都加入到resource中,就能看到target/class目錄下有這些jar包啦

不過jar包有了,仍是會報錯:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project op-realname-test: Compilation failure
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:972)
at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
... 21 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

編譯失敗了,而且是由於缺乏類引發的,並且是maven-compiler-plugin插件報錯,檢查發現,因爲加入了新的資源,打包插件中也須要加入相應包路徑:

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <!-- 打包本地jar包 -->
                        <extdirs>${project.basedir}/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>

done