Maven配置集合帖

Maven构建时包含资源文件

在Netbeans中创建一个Maven项目后,构建出来的Jar默认不带有xml或者png等资源文件,需要手动进行配置一下,将以下配置代码放在pom.xml中的<build>节点下:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </resource>
</resources>

Maven签名设置

对于Applet小桌面程序,因为安全性等问题,需要对Jar进行签名,下面是签名的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jarsigner-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <id>sign</id>
            <phase>test</phase>
            <goals>
                <goal>sign</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <removeExistingSignatures>true</removeExistingSignatures>
        <skip>false</skip>
        <archiveDirectory>target/DaksServer150-15.0-SNAPSHOT/applications/applet/</archiveDirectory>
        <includes>
            <include>**/*SNAPSHOT.jar</include>
        </includes>
        <keystore>${basedir}/src/main/webapp/applications/applet/keystore.jks</keystore>
        <alias>codesign2015</alias>
        <storepass>changeit</storepass>
    </configuration>
</plugin>

我的笔记中还记录了下面这种配置方式,但我都不记得是怎么配置的了,贴出来备忘一下:

<target name="-post-compile">
    <signjar alias="ccrl" keystore="${build.web.dir}//keystore.jks"
             storepass="changeit" lazy="true">
        <path>
            <fileset dir="${build.web.dir}//jsp/hiscroapplet/lib" includes="*.jar" />
            <fileset dir="${build.web.dir}//jsp/hiscroapplet" includes="*.jar" />
            <fileset dir="${build.web.dir}/" includes="*.jar" />
            <fileset dir="${build.web.dir}//lib" includes="*.jar" />
        </path>
    </signjar>
</target>

Maven-将多个工程打包成一个Jar

我们公司有个产品是Applet网站,我们自己在设计模块时,因为分的太细导致有20+的Jar出现,但每个Jar都很小。这在Applet加载Jar时对速度的影响是致命的,所以我们需要把这些Jar打包成一个下载。下面是配置文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <artifactSet>
            <includes>
                <include>com.cc</include>
            </includes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Permissions>all-permissions</Permissions>
                            <Caller-Allowable-Codebase>*</Caller-Allowable-Codebase>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

构建可执行Jar时Main-Class和Class-Path的配置

Maven工程默认产生的Jar却不是可执行的,尤其在你创建了一个桌面应用程序后,不能直接执行是不符合要求的。

于是在本文中,我简单记录下如何配置使得Maven工程构建产生可执行的Jar,这里主要就是对Main-Class和Class-Path的配置。只要将以下代码复制到Maven配置文件pom.xml中,并注意修改MainClass部分即可。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <!--change addClasspath to true if it is desktop app-->
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.daks.timesheethelper.App</mainClass>
                        <addDefaultImplementationEntries>
                          true
                        </addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>
                          true
                        </addDefaultSpecificationEntries>
                    </manifest>
                    <manifestEntries>
                        <Permissions>${Permissions}</Permissions>
                        <Caller-Allowable-Codebase>
                          ${Caller-Allowable-Codebase}
                        </Caller-Allowable-Codebase>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <configuration>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 

分享到:

发表评论

昵称

沙发空缺中,还不快抢~