This article will introduce maven custom plug-ins (entry-combat) archeType custom template (real) environment packaged by (real) in the private servers are often required three operations.
1, custom template archeType
1.1 What is archeType
When we create a maven project, you will find that there are so many apache template provided.
Or use the mvn archetype: generate commands to quickly create a maven project, there will be a number of options that let you choose a template number. What is the difference between that each template?
Each template is actually shipped with different dependencies and plugins. PW in general in the company will have a archeType template belongs to the company, which has a debugging dependencies and version number used in good project.
1.2, create archetype
If they have had a maven project, the project would like to create a archeType template.
cd to execute the project root directory (pom.xml same directory).
At this time, the files will be generated under the project target:
1.3, generate archetype template
先 cdtarget/generated-sources/archetype/
Then execute mvn install
After successful execution, execution crawl command to generate archetype-catalog.xml framework configuration files in the root directory of the local repository:
mvnarchetype:crawl
Take a look at the content inside it:
1.4, using the archetype template
Execute mvn archetype: generate -DarchetypeCatalog = local projects from local archeType create templates.
Then let you select a template and groupId artifactId version number and package information:
Project successfully created!
Of course, you can also use IDEA to help us create a project using archeType templates using a graphical interface:
Back to create a common project with the same, and do presentations.
2, custom plug-ins
I'm just here to do a simple example, the development of more complex functions, please refer to the mojo API:
https://maven.apache.org/developers/mojo-api-specification.html
2.1、插件提供者
插件提供者项目结构:
2.1.1、修改packaging
2.1.2、修改pom
这两个依赖是自定义插件必须的依赖,代表了它是一个Mojo工程,里面包含了一些Mojo的接口和抽象类以及注解。
2.1.3、coding业务逻辑
注意这里面的@Parameter @Mojo LifecyclePhase.PACKAGE都是org.apache.maven.plugins.annotations包下的:
@Parameter注解会获取消费者配置文件中的变量值并赋值。
defaultPhase =LifecyclePhase.PACKAGE声明了该插件触发的生命周期。
@Mojo定义插件的goal名字。
2.1.4、clean and install
执行mvn clean install,在target目录下会生成这样一个jar包,这就是插件包。
2.2、插件消费者
插件消费者 : 项目结构
2.2.1、修改pom
如果不加,我们只能通过执行插件或者执行命令的方式来执行,如果想让它在执行package的时候自动执行,就需要设置该属性,可以把它理解成hook。
2.2.2、如何传递参数给plugin
在插件提供者中,有个MyMojo的类,有这样一段代码:
它和你用过的spring注解一样,也是用来以注解的形式获取参数的值。
相对应的,在插件消费者的配置中我们就应该相应的给出参数的定义:
上面的配置与变量名一一对应即可。这时候你会发现maven插件中自动会添加一个plugins选项:
执行该插件:mvnmyprovide:fantj 或者直接点击:
3、Profile按环境打包
在日常开发中,我们项目的开发环境和生产环境以及测试环境往往是不同的,比如:数据库的url等。在项目上生产环境时,就需要修改这些参数,给开发造成不便。为了解决该问题,Maven 2.0引入了构建配置文件的概念(build profiles)。
它能干什么呢?
If required different environment configuration, the production environment configuration file is pro.properties, development environment configuration file is dev.properties, then use maven profile, you can achieve the development environment jar package when the package will only dev your production and development environments .properties packaging and the use, production environment packaged the same way.
Where a statement it?
It can be declared in the pom.xml file for each project can be declared under maven setting.xml users can also set up maven setting.xml in the global environment, as detailed below.
1.Per Project
Defined in the POM itself (pom.xml).
2.Per User
Defined in the Maven-settings(%USER_HOME%/.m2/settings.xml)
3.Global
Defined in the globalMaven-settings (${maven.home}/conf/settings.xml)
4.Profile descriptor
does not support 3.0, details:
Although there are so many ways define, but we generally use is first defined in the pom, not necessarily because of the production environment for each project are exactly the same, of course, this is due to personal circumstances.
Real
3.1, project structure
3.2, pom.xml
3.3, three application.properties
3.4, packing
We can see only the pro / application.properties been compiled.