美文网首页
maven学习笔记

maven学习笔记

作者: 草珊瑚_6557 | 来源:发表于2019-03-22 11:01 被阅读0次

    开发阶段

    mvn archetype:generate -DgroupId=com.xxx.demo -DartifactId=xxxExample -DarchetypeArtifactId=maven-archetype-quickstart
    

    快速创建一个maven项目。

    mvn compile
    

    项目编译。

    mvn exec:java -Dexec.mainClass="com.xxx.demo.App"
    

    指定一个com.xxx.demo.Appjava类文件作为入口执行项目。

    mvn exec:java -Dexec.mainClass="com.xxx.demo.App" -Dexec.args="arg1 arg2 arg3"
    

    main函数传参方法一,这时main函数的args参数为["arg1","arg2","arg3"]

    mvn exec:java -Dexec.mainClass="com.xxx.demo.App" -Dconfig="../config.json"
    

    main函数传参方法二(推荐),这时需要通过语句System.getProperty("config")来获取config的参数值"../config.json"

    打包

    pom.xml添加build插件配置。

    <project>
      <build>
        <plugins>
    
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <appendAssemblyId>false</appendAssemblyId>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <!-- 此处指定main方法入口的class -->
                  <mainClass>com.xxx.demo.App</mainClass>
                </manifest>
              </archive>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>assembly</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
        </plugins>
      </build>
    </project>
    

    命令行执行maven package便可以在target文件夹下生成对应的jar文件。
    执行的时候输入java -Dconfig="./config.json" -jar xxx.jar即可。

    maven配置

    配置文件路径参考:D:\Program Files\Apache\maven\conf\settings.xml
    配置文件常用配置

    阿里云镜像配置参考

    <mirrors>
        <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>        
        </mirror>
      </mirrors>
    

    servers节点

    用来配置公司私服的上传用户名和密码

    pom.xml文件内容是依赖关系的配置。
    settings.xml文件内容是依赖库的服务器信息(地址、用户名、密码)。
    Maven的全局配置文件路径:%MAVEN_HOME%\conf\settings.xml

    安装指定包

    mvn install:install-file -DgroupId=jfree -DartifactId=jcommon -Dversion=1.0.16 -Dpackaging=jar -Dfile=jcommon-1.0.16.jar
    

    参考:
    https://segmentfault.com/q/1010000005703437/a-1020000005716348
    https://www.cnblogs.com/s1165482267/p/7928275.html

    相关文章

      网友评论

          本文标题:maven学习笔记

          本文链接:https://www.haomeiwen.com/subject/lnosmqtx.html