<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.daxin</groupId> <artifactId>maven_params_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>maven_params_test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <!-- 指定过滤resources下面的application.properties --> <filters> <filter>src/main/resources/application.properties</filter> </filters> <resources> <!--是定义哪些目录下的文件会被配置文件中定义的变量替换 --> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <!-- 指定打包的jar名字 --> <!-- projectName_PROJECTvERSION --> <finalName>${name}_${version}</finalName> <!-- The directory where compiled application classes are placed. 用来存储编译的class文件 --> <outputDirectory>E:codemvnoutput</outputDirectory> <!-- 配置源文件所在位置 --> <sourceDirectory>src/main/java</sourceDirectory> <!-- The directory where all files generated by the build are placed. 其实就是修改默认的target位置 --> <directory>E:codedict</directory> <!-- build中的extensions是执行构建过程中可能用到的其他工lib,在执行构建的过程中被加入到classpath中。 --> <extensions> <extension> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </extension> </extensions> <plugins> <!-- maven打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.daxin.maven.params.test.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <!-- maven 进行打包部署时候可以根据-P 传递参数进行不同环境的配置,例如:开发环境和部署环境的jdbc的host不同的话,可以使用maven profile配置 --> <!-- maven package -P debug 就可以激活id为debug的配置信息 --> <profile> <!-- profile的唯一标识,打包时候可以使用-P参数进行激活 --> <id>debug</id> <properties> <pom.env>debug</pom.env> <jdbc.url>debug_jdbc_host</jdbc.url> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>deploy</id> <properties> <pom.env>deploy</pom.env> <jdbc.url>deploy_jdbc_host</jdbc.url> </properties> </profile> </profiles> </project>
mvn运行java程序,需要使用插件,其中${mainClass}是我们自定义的参数,我们可以使用mvn传入该参数:mvn exec:java -DmainClass=com.daxin.maven.params.test.Main
其中-D表示传入参数,mainClass是属性名字。
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.5.0</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>${mainClass}</mainClass> </configuration> </plugin>