Maven实战读书笔记

2.4 节
有的公司基于安全考虑,要求使用通过安全认证的代理访问因特网。比如,现在有一个IP地址为218.14.227.197,端口为3128的代理服务器,我们就需要在settings.xml配置Http代理。

<setting>
    <proxies>
        <proxy>
            <id>my-proxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>218.14.227.197</host>
            <port>3128</port>
            <!--
            <username>xxx</username>
            <password>yyy</password>
            哪些主机名不需要代理
            <nonProxyHosts>repository.mycom.com|*.google.com</nonProxyHosts>
            -->
        </proxy>
    </proxies>
</setting>
View Code

3.4 节
默认生成的jar包是不能够直接运行的,因为jar文件中的META-INF/MANIFEST.MF没有配置Main-Class。
要生成可执行的jar,可以使用maven-shade-plugin,如下:

<build>
    <plugins>
        <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>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.MainfestResourceTransformer">
                                <mainClass></mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
View Code

java -jar xxx.jar


7.4.2 节
将某个插件目标绑定到生命周期的某个阶段上。
创建项目的源码jar包,maven-source-plugin:jar-no-fork目标可以将主代码打包成jar文件,将其绑定到default生命周期的verify阶段上,在测试完安装构件前创建源码包。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
View Code

7.5.3 节
maven-antrun-plugin:run可以用来在maven中调用Ant任务。
将maven-antrun-plugin:run绑定到多个阶段上,再加上不同的配置,让maven在不同阶段执行不同的任务。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>ant-validate</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>I'm bound to validate phase.</echo>
                        </tasks>
                    </configuration>
                </execution>
                <execution>
                    <id>ant-verify</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>I'm bound to verify phase.</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
View Code

8.3.3 节
依赖范围import只在dependencyManagement元素下才有效果,使用该范围的依赖通常指向一个pom,作用是将目标pom中的dependencyManagement配置导入并合并到当前pom的dependencyManagement配置。
除了继承和复制配置之外,还可以使用import范围依赖将这一配置导入。

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>xxx</groupId>
                <artifactId>yyy</artifactId>
                <version>1.0</version>
                <type>pom</type>
                <!-- import范围的特殊性,一般都指向打包类型为pom的模块 -->
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

8.3.4 节
明确提到在pluginManagement声明插件配置后,在子模块还需声明。


12.1 节
war包里有WEB-INF,WEB-INF下有classes文件夹,还有lib文件夹。classes和lib目录都会在运行时被加入到ClassPath中。
web项目的类和资源文件同一般jar项目一样,默认位置都是src/main/java,src/main/resources。比较特殊的地方是,web项目还有一个web资源目录,其默认位置src/main/webapp。war包中有一个lib目录,但maven项目结构中并没有这样一个目录,因为maven的依赖配置都在pom中。


14.5 节
web项目中有两种资源文件,一般资源文件和web资源文件,前者通过maven-resources-plugin处理,后者通过maven-war-plugin处理。
为web资源目录src/main/webapp/开启过滤:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-beta-1</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <includes>
                            <include>**/*.css</include>
                            <include>**/*.js</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
View Code





























猜你喜欢

转载自www.cnblogs.com/Mike_Chang/p/12588950.html