maven总结

standard-directory-layout

standard-directory-layout

How does the Archetype Plugin know about archetypes?

Location of catalog files The Archetype Plugin knows by default about its internal catalog. It also knows about the local and remote catalogs.

local represents the ~/.m2/archetype-catalog.xml catalog file.

remote represents the https://repo.maven.apache.org/maven2/archetype-catalog.xml catalog file.

The Archetype Plugin can also read catalogs from filesystem/HTTP by providing the path/URL of a catalog file or of a directory containing an archetype-catalog.xml file.

How does the Archetype Plugin know about archetypes?

指定pom.xml位置

mvn -f /path/to/pom.xml

settings默认路径

settings default location

参照maven官方文档,默认会从两个路径加载settings配置文件

1.The Maven install: ${maven.home}/conf/settings.xml

2.A user’s install: ${user.home}/.m2/settings.xml

可以将自己定制的配置覆盖到上述两个路径,这样执行mvn的时候就不需要通过-s来指定settings的路径了

deploy to nexus 发布到nexus私服

参考:

Maven Deploy to Nexus

Security and Deployment Settings

Distribution_Management

  1. 设置发布地址,在项目的pom.xml中添加配置
    <distributionManagement>
        <repository>
            <id>my-releases-repository</id>
            <url>http://127.0.0.1/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>my-snapshots-repository</id>
            <url>http://127.0.0.1/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>
  1. 配置安全认证,配置要写到maven全局配置文件settings.xml
    <servers>
        <server>
          <id>my-releases-repository</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
        <server>
          <id>my-snapshots-repository</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
    </servers>    

然后在项目根目录执行mvn deploy即可完成发布,其他对该项目有依赖的项目就可以通过dependency依赖到该发布结果了。

mvn jetty:run启动jetty后无法编辑静态资源问题

参考

主要原因是jetty会使用内存映射文件来缓存静态文件

配置文件在org.eclipse.jetty.webapp下的webdefault.xml有如下说明

* useFileMappedBuffer * If set to true, it will use mapped file buffer to serve static content * when using NIO connector. Setting this value to false means that * a direct buffer will be used instead of a mapped file buffer. * By default, this is set to true.

参考文章中的解决方法是在pom.xml文件中将

implementation=”org.eclipse.jetty.server.nio.SelectChannelConnector”>替换为

implementation=”org.eclipse.jetty.server.bio.SocketConnector”>

修改前

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.8.v20121106</version>
    <configuration>
        <stopKey>stop</stopKey>
        <stopPort>5599</stopPort>
        <webAppConfig>
            <contextPath>/</contextPath>
            <defaultsDescriptor></defaultsDescriptor>
        </webAppConfig>
        <scanIntervalSeconds>5</scanIntervalSeconds>
        <connectors>
            <connector
                implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8080</port>
                <maxIdleTime>60000</maxIdleTime>
            </connector>
        </connectors>
    </configuration>
</plugin>

修改后

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.8.v20121106</version>
    <configuration>
        <stopKey>stop</stopKey>
        <stopPort>5599</stopPort>
        <webAppConfig>
            <contextPath>/</contextPath>
            <defaultsDescriptor></defaultsDescriptor>
        </webAppConfig>
        <scanIntervalSeconds>5</scanIntervalSeconds>
        <connectors>
            <connector
                implementation="org.eclipse.jetty.server.bio.SocketConnector">
                <port>8080</port>
                <maxIdleTime>60000</maxIdleTime>
            </connector>
        </connectors>
    </configuration>
</plugin>

搭建nexus私服

环境:centos 7 minimal 64位+jdk 8+maven3.3.3+git2.5.0

  1. 下载安装maven

download

install

  1. 安装jdk

yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 -y

  1. clone nexus源码

git clone git@github.com:sonatype/nexus-public.git

  1. 编译nexus

进入nexus目录,执行./mvnw clean install,等待网速慢的时候时间较长

  1. 进入target/nexus-**/bin 执行./nexus start完成启动,按ctrl+c然后输入shutdown可以完成关闭

  2. 访问http://localhost:8081/进入管理界面

可以通过修改 target/nexus-**/etc/org.sonatype/.nexus.cfg文件设置端口

配置maven使用本地jar包

  1. dependencies中增加dependency节点

  2. dependencyManagement>dependencies中增加dependency节点

例如导入sqlserver驱动包如下:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sqljdbc4.jar</systemPath>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.4.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>