Maven之maven插件(有讲到tool.jar找不到的解决办法)

来源:互联网 发布:php建行支付接口demo 编辑:程序博客网 时间:2024/05/16 10:05

这是接上一篇文章,上一篇文章太长了,这是接着上一篇文章主要讲一些maven的插件的使用。
1、SQL Maven Plugin

  • 在tian-parent中添加
<plugin>                    <groupId>org.codehaus.mojo</groupId>                    <artifactId>sql-maven-plugin</artifactId>                    <version>1.5</version>                    <!-- 使用插件依然可以指定相应的依赖 -->                    <dependencies>                        <dependency>                            <groupId>mysql</groupId>                            <artifactId>mysql-connector-java</artifactId>                            <version>5.1.38</version>                        </dependency>                    </dependencies>                    <configuration>                        <driver>${jdbc.driver}</driver>                        <url>${jdbc.url}</url>                        <username>${jdbc.username}</username>                        <password>${jdbc.password}</password>                    </configuration>                    <executions>                        <execution>                            <id>create-db</id>                            <phase>compile</phase>                            <goals>                                <goal>execute</goal>                            </goals>                            <configuration>                                <sqlCommand>create database IF NOT EXISTS tian_maven_sqlPlugin</sqlCommand>                            </configuration>                        </execution>                        <execution>                            <id>init-table</id>                            <phase>test-compile</phase>                            <goals>                                <goal>execute</goal>                            </goals>                            <configuration>                                <srcFiles>                                    <srcFile>src/main/resources/init.sql</srcFile>                                </srcFiles>                            </configuration>                        </execution>                    </executions>                </plugin>

src/main/resources/init.sql文件内容如下:

use tian_maven_sqlPlugin;create table if not exists t_user(    id int(10) auto_increment primary key,    username varchar(20),    password varchar(20));
  • 在tian-core的pom.xml上使用这个插件
<plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>sql-maven-plugin</artifactId>            </plugin>
  • 在tian-core上运行clean test
    到mysql数据库中发现:
    这里写图片描述

完全搞定了。

补充:
在这里大家会发现在tian-core的pom上的“parent”标签上有个错误提示,这个maven中eclipse的错误,解决办法,在tian-parent的pom.xml中加入:

<plugin>                    <groupId>org.eclipse.m2e</groupId>                    <artifactId>lifecycle-mapping</artifactId>                    <version>1.0.0</version>                    <configuration>                        <lifecycleMappingMetadata>                            <pluginExecutions>                                <pluginExecution>                                    <pluginExecutionFilter>                                        <groupId>org.codehaus.mojo</groupId>                                        <artifactId>sql-maven-plugin</artifactId>                                        <versionRange>[1.5,)</versionRange>                                        <goals>                                            <goal>execute</goal>                                        </goals>                                    </pluginExecutionFilter>                                    <action>                                        <execute />                                    </action>                                </pluginExecution>                            </pluginExecutions>                        </lifecycleMappingMetadata>                    </configuration>                </plugin>

把项目maven下更新,就没有了错误了。


2、cobertura-maven-plugin

这个插件是生成测试覆盖率的表结果的插件,开发人员可以参考,是否有其他没有测试的到的地方。
方法一:
由于新版本的maven都集成了cobertura插件,所以只要运行 cobertura:cobertura命令就会自动下载插件
方法二:
在plugin中绑定到某一个声明周期中,这样运行某一个命令(如 test 、compile等)就会运行这个插件。方法我等会儿介绍,在这个地方出了一个错误,搞了我好多天,我都快疯了,网上各方,google上各种方法,就是没有一个起作用的,最后自己综合这些方法,算是解决了。也算是以后对这类情况的解决方法吧。
错误提示:

Failed to execute goal org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument (cobertura-report) on project tian-core: Execution cobertura-report of goal org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument failed: Plugin org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:0 at specified path D:\win8JDK/../lib/tools.jar

老是提示我找不到jdk下的tools.jar包,这个确实在我的jdk配置的JAVA_HOME也是jdk下的bin,不是jre,网上按照这个思路尝试了就是不行,还有就是在pom.xml的建个依赖,吧这个依赖的系统路径指向jdk下的这个tool.jar网上好人成功了,包括好多老外也是成功了,不知道为啥,我就是没有成功……
最后,我也是没有办法了,pom.xml依赖不都下到本地的仓库了吗,我就在我的本地仓库,
步骤一:
建立一个tool.jar的仓库包目录:比如我的m2\repository的仓库建里建的目录是:com\sun\tools\1.8.0这里由于我的jdk是1.8.0版本的,所有我就吧tool.jar的名字改为了tool-1.8.0.jar,并吧这个jar拷贝到上面的文件中。
步骤二:
在tian-parent中的依赖管理中添加如下所示:
这里写图片描述
还是在它的插件管理中添加添加插件时,插件中也要添加依赖,如下先源码后图:

<plugin>                    <groupId>org.codehaus.mojo</groupId>                    <artifactId>cobertura-maven-plugin</artifactId>                    <version>2.7</version>                    <dependencies>                        <dependency>                            <groupId>com.sun</groupId>                            <artifactId>tools</artifactId>                            <version>1.8.0</version>                        </dependency>                    </dependencies>                    <configuration>                        <formats>                            <format>html</format>                            <format>xml</format>                        </formats>                    </configuration>                    <executions>                        <execution>                            <id>cobertura-report</id>                            <goals>                                <goal>cobertura</goal>                            </goals>                            <phase>test</phase>                        </execution>                    </executions>                </plugin>

图:
这里写图片描述

步骤三:
在你要执行的看测试覆盖率的子项目(比如tian-core)的pom.xml文件中,依赖中添加:

<dependency>            <groupId>com.sun</groupId>            <artifactId>tools</artifactId>        </dependency>

还是在他的插件中添加:

<plugin>    <groupId>org.codehaus.mojo</groupId>    <artifactId>cobertura-maven-plugin</artifactId></plugin>

我测试了,这三个步骤很中,缺少哪一个都不行,不然还是提示上面的错误!!!!!

最后,在tian-core的pom上执行 clean test/package在BUILD SUCCESS后,左侧的的项目目录中,我们可以看到如图,在浏览器中打开index.html就可以看到我们的测试覆盖率了。
这里写图片描述
这里写图片描述

0 0
原创粉丝点击