Menu

  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay
  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay

Maven POM文件配置示例

22
Jul
2009

Maven POM文件配置示例

By Alex
/ in Java
/ tags Maven, POM
0 Comments
排除传递性依赖
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.5.0</version>
    <exclusions>
        <exclusion>
            <!-- CXF对Spring-web的依赖被排除 -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </exclusion>
    </exclusions>
</dependency>
指定资源文件目录并过滤
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <!-- 在这里可以指定多个资源文件目录 -->
            <directory>src/main/java</directory>
            <excludes>
                <!--注意该配置从本应当是源代码的目录中过滤掉Java文件,其他文件作为资源文件 -->
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
</build>
指定额外的源代码目录
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/birt/java</source>
          <source>src/cms/java</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
过滤资源文件
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
<build>
    <resources>
        <resource>
            <!-- 启用src/main/resources的资源过滤 -->
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <excludes>
                <!-- 排除资源文件 -->
                <exclude>application-${environment}.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>
配置目录链接(Linked Folder)
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <linkedResources>
            <linkedResource>
                <name>src/main/webapp</name>
                <type>2</type>
                <location>D:/www</location>
            </linkedResource>
        </linkedResources>
    </configuration>
</plugin>
打包时生成*–sources.jar
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
        <attach>true</attach>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
打包时生成*–javadoc.jar
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-javadoc-plugin</artifactId>
     <executions>
         <execution>
              <id>attach-javadocs</id>
               <goals>
                   <goal>jar</goal>
                </goals>
          </execution>
       </executions>
</plugin>
指定编译级别
XHTML
1
2
3
4
5
6
7
8
9
10
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
        <source>1.5</source>
        <target>1.5</target>
        <debug>true</debug>
    </configuration>
</plugin>
启用对AspectJ的支持
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>compile</id>
            <configuration>
                <XnoInline>true</XnoInline>
                <forceAjcCompile>true</forceAjcCompile>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.5</source>
                <target>1.5</target>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <outxml>true</outxml>
                <aspectLibraries>
                    <aspectLibrary>
                        <!--下面的构件同时需要在dependency里面配置-->
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
指定源代码、资源文件的编码方式
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
打包时拷贝所有依赖包
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin> 
打包生成可执行的jar文件
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>cc.gmem.common.Application</mainClass>
                    </manifest>
                </archive>
                <appendAssemblyId>true</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>
打uber-jar另一种方法

maven-shade-plugin也支持将依赖打包进JAR,这类似于maven-assembly-plugin。但是后者存在BUG,会导致Unable to locate Spring NamespaceHandler for XML schema namespace这样的错误。

除了打包指外,maven-shade-plugin还具有重命名(Shade)功能 —— 修改某些包名。

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- 禁止生成dependency-reduced-pom.xml -->
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                </transformers>
                <filters>
                    <!-- 解决报错:Invalid signature file digest for Manifest main attributes -->
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>
使用profile打带分类器(classifer)的jar包
Shell
1
2
#分类器打包命令示例
mvn clean jar -P jdk15

XHTML
1
2
3
4
5
6
7
<!-- 引用带分类器的构件 -->
<dependency>
  <groupId>cc.gmem.tools</groupId>
  <artifactId>cmd-tools</artifactId>
  <version>1.0</version>
  <classifier>jdk15</classifier>
</dependency>

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${jar.source}</source>
                    <target>${jar.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- 执行mvn命令时,使用-P参数激活某个profile -->
    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <jar.source>1.6</jar.source>
                <jar.target>1.6</jar.target>
            </properties>
        </profile>
        <profile>
            <id>jdk15</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>jdk15</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <jar.source>1.5</jar.source>
                <jar.target>1.5</jar.target>
            </properties>
        </profile>
        <profile>
            <id>jdk14</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>jdk14</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <jar.source>1.4</jar.source>
                <jar.target>1.4</jar.target>
            </properties>
        </profile>
    </profiles>
</project>
执行Junit测试控制台输出乱码问题
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <dependencies>
        <dependency>
            <groupId> org.apache.maven.surefire</groupId>
            <artifactId> surefire-junit47</artifactId>
            <version> 2.15</ version>
        </dependency>
    </dependencies>
    <configuration>
        <argLine> -Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>
将war构件中的Java类独立打包
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <!-- 下面的配置参数导致所有Java类被打包到一个JAR文件,并嵌入在WEB-INF/LIB下 -->
        <archiveClasses>true</archiveClasses>
        <!--
        下面的配置参数导致所有Java类被导报到一个JAR文件,并作为额外的构件看待
        该构件的名称为[groupId].[artifactId].[version]-classes.jar
        其它项目可以通过分类器classes来引用此JAR:
             <groupId>myGroup</groupId>
             <artifactId>myArtifact</artifactId>
             <version>myVersion</myVersion>
             <classifier>classes</classifier>
        -->
        <attachClasses>true</attachClasses>
        <!-- 上述额外构件的分类器字段的名称,默认classes -->
        <classesClassifier>classes</classesClassifier>
    </configuration>
</plugin>
将多个war包构件的内容合并到一个中
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<dependencies>
    <dependency>
        <groupId>cc.gmem.secmon</groupId>
        <artifactId>map</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
        <scope>runtime</scope>
        <!-- 以这种方式声明war依赖后,当前项目打包后,该war包的全部内容都会叠加进去-->
    </dependency>
</dependencies>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <overlays>
            <!-- 使用下面的配置,可以指定war运行时依赖中,哪些内容需要被排除出去 -->
            <!--下面的构件同时需要在dependency里面配置-->
            <overlay>
                <groupId>cc.gmem.secmon</groupId>
                <artifactId>map</artifactId>
                <excludes>
                    <exclude>WEB-INF/lib/sshe-*.jar</exclude><!-- 通配单个目录或者文件的一部分 -->
                    <exclude>WEB-INF/classes/**/*.class</exclude> <!-- 双星号通配任意层目录 -->
                </excludes>
            </overlay>
            <overlay>
                <groupId>cc.gmem.secmon</groupId>
                <artifactId>intelli</artifactId>
                <excludes>
                    <exclude>WEB-INF/lib/comm-tools-*.jar</exclude>
                </excludes>
            </overlay>
        </overlays>
    </configuration>
</plugin>
指定Maven仓库的位置,在mvn deploy时用到
XHTML
1
2
3
4
5
6
7
8
9
10
<distributionManagement>
    <repository>
        <id>nexus</id>
        <url>http://192.168.0.200:8801/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
        <id>nexus</id>
        <url>http://192.168.0.200:8801/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
</distributionManagement>

注:在org.apache.maven.plugins:maven-deploy-plugin:2.8.2版本之后,不配置distributionManagement也可以mvn deploy,可以通过命令行指定远程仓库的信息。使用如下系统属性即可:

Shell
1
2
3
                                 #            id::layout::url
-DaltReleaseDeploymentRepository=pa-releases::default::https://nexus.pacloud.io/repository/maven-releases/
-DaltSnapshotDeploymentRepository=pa-snapshots::default::https://nexus.pacloud.io/repository/maven-snapshots/
压缩和混淆JavaScript代码
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.5.1</version>
    <executions>
        <execution>
            <phase>process-resources</phase><!-- 注意,该阶段在Emvn package -Pproclipse中会自动运行,一旦你保存源码,就会执行压缩 -->
            <goals>
                <goal>compress</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <nosuffix>true</nosuffix> <!-- 输出js是否不带-min.js后缀 -->
        <!-- 压缩文件存放目录 -->
        <webappDirectory>${project.build.directory}/compressed</webappDirectory>
        <jswarn>false</jswarn> <!-- 是否显示警告信息 -->
        <nocompress>false</nocompress><!-- 是否压缩 -->
        <nomunge>false</nomunge><!-- 是否进行代码混淆 -->
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <webResources>
            <resource>
                <!-- 这里必须引用压缩文件存放目录 -->
                <directory>${project.build.directory}/compressed</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

上面的配置,在Eclipse中会导致每次保存JavaScript文件都执行压缩,这会影响性能,因此可以使用Maven的Profile机制,仅在打包时启用压缩,下面是一个例子:

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <overlays>
                    <overlay>
                        <groupId>cc.gmem.study</groupId>
                        <artifactId>obfuscate-module</artifactId>
                        <excludes>
                            <exclude>WEB-INF/classes/**/*.class</exclude>
                        </excludes>
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>production</id>
        <properties>
            <yui.compressed.dir>${project.build.directory}/compressed</yui.compressed.dir>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>yuicompressor-maven-plugin</artifactId>
                    <version>1.5.1</version>
                    <executions>
                        <execution>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>compress</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <nosuffix>true</nosuffix> <!-- 输出js是否不带-min.js后缀 -->
                        <!-- 压缩文件存放目录 -->
                        <webappDirectory>${yui.compressed.dir}</webappDirectory>
                        <jswarn>false</jswarn> <!-- 是否显示警告信息 -->
                        <nocompress>false</nocompress><!-- 是否压缩 -->
                        <nomunge>false</nomunge><!-- 是否进行代码混淆 -->
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <!-- overlays的配置会从默认Profile继承,不需要再次声明 -->
                        <webResources>
                            <resource>
                                <!-- 这里必须引用压缩文件存放目录 -->
                                <directory>${yui.compressed.dir}</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
压缩和混淆Java代码
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.8</version>
    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>5.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <!-- 注意,不会对maven-war-plugin通过overlay得到的类进行任何处理,此时war已经打包完毕 -->
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
            <configuration>
                <!-- 从文件读取ProGuard配置选项 -->
                <proguardInclude>${project.basedir}/.proguard</proguardInclude>
                <!-- 直接指定ProGuard配置选项 -->
                <options>
                    <option>-keep public class * { public protected *; }</option>
                    <option>-dontoptimize</option>
                </options>
                <!--
                    以下ProGuard选项自动设置:
                    -dontobfuscate,如果配置obfuscate
                    -verbose,如果以-X运行Maven
                -->
                
                <!-- JVM库一般不在Maven依赖列表中 -->
                <libs>
                    <lib>${java.home}/lib/rt.jar</lib>
                    <lib>${java.home}/lib/jsse.jar</lib>
                </libs>
                <!-- 是否把Maven依赖作为库看待,默认true -->
                <includeDependency>true</includeDependency>
                <!-- 当启用includeDependency时,进行细粒度的过滤控制 -->
                <exclusions>
                    <exclusion>
                        <groupId>javax.inject</groupId>
                        <artifactId>javax.inject</artifactId>
                    </exclusion>
                </exclusions>
                <!-- 执行代码混淆-->
                <obfuscate>true</obfuscate>
                
                <!-- START
                    下面的配置,将把编译阶段得出的classes拷贝daoclasses_proguard_base,
                    然后把daoclasses_proguard_base作为输入目录;把classes作为输出目录
                -->
                <!-- 输入文件设置为编译好的target/classes目录 -->
                <injar>classes</injar>
                <!-- 不匹配下面过滤器的文件,不会被处理,也不会进入classes -->
                <inFilter>!cc/gmem/study/obfuscate/keep/**</inFilter>
                <!-- END -->
                
                <!-- START
                    如上配置,如果加上下面这行设置,则classes保持编译阶段的原样,
                    处理后的结果存放到processed-classes
                -->
                <outjar>processed-classes</outjar>
                <!-- END -->
                
            </configuration>
        </execution>
    </executions>
</plugin>

使用Spring、Hibernate的工程,混淆配置如下:

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<execution>
    <phase>process-classes</phase>
    <goals>
        <goal>proguard</goal>
    </goals>
    <configuration>
        <injar>classes</injar>
        <options>
            <option>
                -libraryjars ${java.home}/lib/rt.jar
                -keep public class * { public protected *; }
                -keepparameternames  #例如SpringMVC,可能依赖于方法参数的名称
                -keepattributes Exceptions,InnerClasses,Signature,*Annotation*,EnclosingMethod
                -keepclassmembers class * implements java.io.Serializable {
                    static final long serialVersionUID;
                    private static final java.io.ObjectStreamField[] serialPersistentFields;
                    private void writeObject(java.io.ObjectOutputStream);
                    private void readObject(java.io.ObjectInputStream);
                    java.lang.Object writeReplace();
                    java.lang.Object readResolve();
                }
                -keepclassmembers class * {
                    @javax.annotation.Resource *;
                }
                -keepclassmembers class * {
                    @javax.inject.Inject *;
                }
                -keepclassmembers class * {
                    @org.springframework.beans.factory.annotation.Autowired *;
                }
            </option>
        </options>
    </configuration>
</execution>
spring boot
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.0.RELEASE</version>
    <configuration>
        <!-- 指定入口点 -->
        <mainClass>com.dangdang.digital.spring.data.redis.Application</mainClass>
        <layout>JAR</layout>
        <!-- 将主资源文件目录加入到classpath中 -->
        <addResources>true</addResources>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<!--
    打自包含包
    mvn clean package -P boot
    运行程序
    mvn spring-boot:run -P boot
-->
集成docker
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.7</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>
                <goal>push</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- Docker上下文目录,寻找其中的Dockerfile文件,不支持非Dockerfile的文件名 -->
        <contextDirectory>.</contextDirectory>
        <!-- 打包的Docker镜像名称 -->
        <repository>docker.gmem.cc/prometheus/dubbo_exporter</repository>
        <tag>${project.version}</tag>
        <!-- 构建时参数,必须在Dockerfile中声明对应的ARG -->
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>
集成helm

该插件支持打包、部署、删除,不支持和Chart仓库交互。 

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
    <groupId>org.microbean</groupId>
    <artifactId>helm-maven-plugin</artifactId>
    <version>2.8.2.1.1.1</version>
    <executions>
        <execution>
            <id>default</id>
            <phase>package</phase>
            <goals>
                <goal>package</goal>
            </goals>
            <configuration>
                <!-- Chart定义存在的目录 -->
                <chartContentsUri>file:${project.basedir}/.chart</chartContentsUri>
                <!-- 打包后的Chart存放位置 -->
                <chartTargetUri>file:${project.build.directory}/charts/${project.artifactId}.tgz</chartTargetUri>
            </configuration>
        </execution>
    </executions>
</plugin>
集成bash
XML
1
2
3
4
5
6
7
8
9
10
11
<plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>bash-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
        <script>
            helm delete dubbo-exporter --purge
            helm install target/charts/dubbo_exporter.tgz --name=dubbo-exporter
        </script>
    </configuration>
</plugin>
Groovy + Java 联合

添加插件仓库:

XML
1
2
3
4
5
6
7
8
9
10
11
<pluginRepository>
    <id>bintray</id>
    <name>Groovy Bintray</name>
    <url>https://dl.bintray.com/groovy/maven</url>
    <releases>
        <updatePolicy>never</updatePolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</pluginRepository>

 添加插件:

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <compilerId>groovy-eclipse-compiler</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>3.3.0-01</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>2.5.6-01</version>
        </dependency>
    </dependencies>
</plugin>

Groovy的依赖仍然需要添加:

XML
1
2
3
4
5
6
<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy</artifactId>
  <classifier>indy</classifier>
  <version>2.5.6</version>
</dependency>

关于源码目录结构:

  1. 默认情况下,可以把所有Groovy代码都放到src/main/java目录
  2. 如果src/main/java下至少有一个文件,则可以将Groovy代码放到src/main/groovy目录 
← Hibernate基于XML的配置文件样例
SVN知识集锦 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Related Posts

  • Maven知识集锦
  • Maven依赖速查表
  • Maven原型系统
  • Tomcat知识集锦
  • log4j配置文件样本

Recent Posts

  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
  • A Comprehensive Study of Kotlin for Java Developers
  • 背诵营笔记
  • 利用LangChain和语言模型交互
  • 享学营笔记
ABOUT ME

汪震 | Alex Wong

江苏淮安人,现居北京。目前供职于腾讯云,专注容器方向。

GitHub:gmemcc

Git:git.gmem.cc

Email:gmemjunk@gmem.cc@me.com

ABOUT GMEM

绿色记忆是我的个人网站,域名gmem.cc中G是Green的简写,MEM是Memory的简写,CC则是我的小天使彩彩名字的简写。

我在这里记录自己的工作与生活,同时和大家分享一些编程方面的知识。

GMEM HISTORY
v2.00:微风
v1.03:单车旅行
v1.02:夏日版
v1.01:未完成
v0.10:彩虹天堂
v0.01:阳光海岸
MIRROR INFO
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Recent Posts
  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
    In this blog post, I will walk ...
  • A Comprehensive Study of Kotlin for Java Developers
    Introduction Purpose of the Study Understanding the Mo ...
  • 背诵营笔记
    Day 1 Find Your Greatness 原文 Greatness. It’s just ...
  • 利用LangChain和语言模型交互
    LangChain是什么 从名字上可以看出来,LangChain可以用来构建自然语言处理能力的链条。它是一个库 ...
  • 享学营笔记
    Unit 1 At home Lesson 1 In the ...
  • K8S集群跨云迁移
    要将K8S集群从一个云服务商迁移到另外一个,需要解决以下问题: 各种K8S资源的迁移 工作负载所挂载的数 ...
  • Terraform快速参考
    简介 Terraform用于实现基础设施即代码(infrastructure as code)—— 通过代码( ...
  • 草缸2021
    经过四个多月的努力,我的小小荷兰景到达极致了状态。

  • 编写Kubernetes风格的APIServer
    背景 前段时间接到一个需求做一个工具,工具将在K8S中运行。需求很适合用控制器模式实现,很自然的就基于kube ...
  • 记录一次KeyDB缓慢的定位过程
    环境说明 运行环境 这个问题出现在一套搭建在虚拟机上的Kubernetes 1.18集群上。集群有三个节点: ...
  • eBPF学习笔记
    简介 BPF,即Berkeley Packet Filter,是一个古老的网络封包过滤机制。它允许从用户空间注 ...
  • IPVS模式下ClusterIP泄露宿主机端口的问题
    问题 在一个启用了IPVS模式kube-proxy的K8S集群中,运行着一个Docker Registry服务 ...
  • 念爷爷
      今天是爷爷的头七,十二月七日、阴历十月廿三中午,老人家与世长辞。   九月初,回家看望刚动完手术的爸爸,发

  • 6 杨梅坑

  • liuhuashan
    深圳人才公园的网红景点 —— 流花山

  • 1 2020年10月拈花湾

  • 内核缺陷触发的NodePort服务63秒延迟问题
    现象 我们有一个新创建的TKE 1.3.0集群,使用基于Galaxy + Flannel(VXLAN模式)的容 ...
  • Galaxy学习笔记
    简介 Galaxy是TKEStack的一个网络组件,支持为TKE集群提供Overlay/Underlay容器网 ...
TOPLINKS
  • Zitahli's blue 91 people like this
  • 梦中的婚礼 64 people like this
  • 汪静好 61 people like this
  • 那年我一岁 36 people like this
  • 为了爱 28 people like this
  • 小绿彩 26 people like this
  • 彩虹姐姐的笑脸 24 people like this
  • 杨梅坑 6 people like this
  • 亚龙湾之旅 1 people like this
  • 汪昌博 people like this
  • 2013年11月香山 10 people like this
  • 2013年7月秦皇岛 6 people like this
  • 2013年6月蓟县盘山 5 people like this
  • 2013年2月梅花山 2 people like this
  • 2013年淮阴自贡迎春灯会 3 people like this
  • 2012年镇江金山游 1 people like this
  • 2012年徽杭古道 9 people like this
  • 2011年清明节后扬州行 1 people like this
  • 2008年十一云龙公园 5 people like this
  • 2008年之秋忆 7 people like this
  • 老照片 13 people like this
  • 火一样的六月 16 people like this
  • 发黄的相片 3 people like this
  • Cesium学习笔记 90 people like this
  • IntelliJ IDEA知识集锦 59 people like this
  • 基于Kurento搭建WebRTC服务器 38 people like this
  • Bazel学习笔记 37 people like this
  • PhoneGap学习笔记 32 people like this
  • NaCl学习笔记 32 people like this
  • 使用Oracle Java Mission Control监控JVM运行状态 29 people like this
  • Ceph学习笔记 27 people like this
  • 基于Calico的CNI 27 people like this
Tag Cloud
ActiveMQ AspectJ CDT Ceph Chrome CNI Command Cordova Coroutine CXF Cygwin DNS Docker eBPF Eclipse ExtJS F7 FAQ Groovy Hibernate HTTP IntelliJ IO编程 IPVS JacksonJSON JMS JSON JVM K8S kernel LB libvirt Linux知识 Linux编程 LOG Maven MinGW Mock Monitoring Multimedia MVC MySQL netfs Netty Nginx NIO Node.js NoSQL Oracle PDT PHP Redis RPC Scheduler ServiceMesh SNMP Spring SSL svn Tomcat TSDB Ubuntu WebGL WebRTC WebService WebSocket wxWidgets XDebug XML XPath XRM ZooKeeper 亚龙湾 单元测试 学习笔记 实时处理 并发编程 彩姐 性能剖析 性能调优 文本处理 新特性 架构模式 系统编程 网络编程 视频监控 设计模式 远程调试 配置文件 齐塔莉
Recent Comments
  • qg on Istio中的透明代理问题
  • heao on 基于本地gRPC的Go插件系统
  • 黄豆豆 on Ginkgo学习笔记
  • cloud on OpenStack学习笔记
  • 5dragoncon on Cilium学习笔记
  • Archeb on 重温iptables
  • C/C++编程:WebSocketpp(Linux + Clion + boostAsio) – 源码巴士 on 基于C/C++的WebSocket库
  • jerbin on eBPF学习笔记
  • point on Istio中的透明代理问题
  • G on Istio中的透明代理问题
  • 绿色记忆:Go语言单元测试和仿冒 on Ginkgo学习笔记
  • point on Istio中的透明代理问题
  • 【Maven】maven插件开发实战 – IT汇 on Maven插件开发
  • chenlx on eBPF学习笔记
  • Alex on eBPF学习笔记
  • CFC4N on eBPF学习笔记
  • 李运田 on 念爷爷
  • yongman on 记录一次KeyDB缓慢的定位过程
  • Alex on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • haolipeng on 基于本地gRPC的Go插件系统
  • 吴杰 on 基于C/C++的WebSocket库
©2005-2025 Gmem.cc | Powered by WordPress | 京ICP备18007345号-2