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

这是接上一篇文章,上一篇文章太长了,这是接着上一篇文章主要讲一些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就可以看到我们的测试覆盖率了。
这里写图片描述
这里写图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值