当前位置: 代码网 > it编程>编程语言>Java > maven 离线模式打包的实现示例

maven 离线模式打包的实现示例

2026年01月21日 Java 我要评论
遇到问题由于客户对于安全性的要求比较高 又想要自己打包,但不允许使用外网,这样就出现了没办法在线打包,需要离线打包。其实也很简单,只需要三步。第一步删除仓库中的_remote.repositories

遇到问题

由于客户对于安全性的要求比较高 又想要自己打包,但不允许使用外网,这样就出现了没办法在线打包,需要离线打包。其实也很简单,只需要三步。

第一步

删除仓库中的_remote.repositories文件,否则还是回到远程仓库去找(重要重要重要)
windows环境下

打开仓库地址,输入cmd 打开命令行,执行下面语句
for /r %i in (_remote.repositories) do del %i

第二步

将项目中用到的依赖,将本地仓库打包发送给客户。

第三步

复制一个maven配置文件,将里面的在线地址相关的全部注释掉,setting配置相关调整

<!--  仓库地址(本地)  -->
<localrepository>d:\workspace\zhengrepo\repo</localrepository>

<!-- 离线模式开启 -->
<offline>true</offline>

<!-- 将镜像地址设置为本地maven地址 -->
<mirror>
	<id>central</id>
	<name>central</name>
	<url>file://d:\workspace\zhengrepo\repo</url>
	<mirrorof>*</mirrorof>
</mirror>

第四步

执行命令

mvn clean package -o -dmaven.test.skip=true -p 要打包的环境
例子
mvn clean package -o -dmaven.test.skip=true -p product

完成的setting.xm配置文件

<!-- 
licensed to the apache software foundation (asf) under one
or more contributor license agreements.  see the notice file
distributed with this work for additional information
regarding copyright ownership.  the asf licenses this file
to you under the apache license, version 2.0 (the
"license"); you may not use this file except in compliance
with the license.  you may obtain a copy of the license at

    http://www.apache.org/licenses/license-2.0

unless required by applicable law or agreed to in writing,
software distributed under the license is distributed on an
"as is" basis, without warranties or conditions of any
kind, either express or implied.  see the license for the
specific language governing permissions and limitations
under the license.
 -->
<!-- 
 | this is the configuration file for maven. it can be specified at two levels:
 |
 |  1. user level. this settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 note: this location can be overridden with the cli option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. global level. this settings.xml file provides configuration for all maven
 |                 users on a machine (assuming they're all using the same maven
 |                 installation). it's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 note: this location can be overridden with the cli option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | the sections in this sample file are intended to give you a running start at
 | getting the most out of your maven installation. where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 | -->
<settings xmlns="http://maven.apache.org/settings/1.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/settings/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--  localrepository
     | the path to the local repository maven will use to store artifacts.
     |
     | default: ${user.home}/.m2/repository
    <localrepository>/path/to/local/repo</localrepository>
     -->
<!-- <localrepository>d:\workspace\zhengrepo\repo</localrepository> -->
<!--  仓库地址(现场)  -->
<!-- <localrepository>d:\feng\repo</localrepository>        -->
<!--  仓库地址(本地)  -->
<localrepository>d:\workspace\zhengrepo\repo</localrepository>
<!--  interactivemode
     | this will determine whether maven prompts you when it needs input. if set to false,
     | maven will use a sensible default value, perhaps based on some other setting, for
     | the parameter in question.
     |
     | default: true
    <interactivemode>true</interactivemode>
     -->
<!--  offline
     | determines whether maven should attempt to connect to the network when executing a build.
     | this will have an effect on artifact downloads, artifact deployment, and others.
     |
     | default: false
    <offline>false</offline>
     -->
	 <!-- 离线模式开启 -->
	 <offline>true</offline>
	
<!--  plugingroups
     | this is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
     | when invoking a command line like "mvn prefix:goal". maven will automatically add the group identifiers
     | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
     | -->
<plugingroups>
<!--  plugingroup
         | specifies a further group identifier to use for plugin lookup.
        <plugingroup>com.your.plugins</plugingroup>
         -->
</plugingroups>
<!--  proxies
     | this is a list of proxies which can be used on this machine to connect to the network.
     | unless otherwise specified (by system property or command-line switch), the first proxy
     | specification in this list marked as active will be used.
     | -->
<proxies>
<!--  proxy
         | specification for one proxy, to be used in connecting to the network.
         |
        <proxy>
          <id>optional</id>
          <active>true</active>
          <protocol>http</protocol>
          <username>proxyuser</username>
          <password>proxypass</password>
          <host>proxy.host.net</host>
          <port>80</port>
          <nonproxyhosts>local.net|some.host.com</nonproxyhosts>
        </proxy>
         -->
</proxies>
<!--  servers
     | this is a list of authentication profiles, keyed by the server-id used within the system.
     | authentication profiles can be used whenever maven must make a connection to a remote server.
     | -->
<servers>
<!--  server
         | specifies the authentication information to use when connecting to a particular server, identified by
         | a unique name within the system (referred to by the 'id' attribute below).
         |
         | note: you should either specify username/password or privatekey/passphrase, since these pairings are
         |       used together.
         |
        <server>
          <id>deploymentrepo</id>
          <username>repouser</username>
          <password>repopwd</password>
        </server>
         -->
<!--  another sample, using keys to authenticate.
        <server>
          <id>siteserver</id>
          <privatekey>/path/to/private/key</privatekey>
          <passphrase>optional; leave empty if not used.</passphrase>
        </server>
         -->
</servers>
<!--  mirrors
     | this is a list of mirrors to be used in downloading artifacts from remote repositories.
     |
     | it works like this: a pom may declare a repository to use in resolving certain artifacts.
     | however, this repository may have problems with heavy traffic at times, so people have mirrored
     | it to several places.
     |
     | that repository definition will have a unique id, so we can create a mirror reference for that
     | repository, to be used as an alternate download site. the mirror site will be the preferred
     | server for that repository.
     | -->
<mirrors>

	<!-- <mirror> -->
		<!-- <id>alimaven</id> -->
		<!-- <name>aliyun maven</name> -->
		<!-- <url>http://maven.aliyun.com/nexus/content/groups/public/</url> -->
		<!-- <mirrorof>central</mirrorof> -->
	<!-- </mirror> -->
	
	<!-- 将镜像地址设置为本地maven地址 -->
	<mirror>
		<id>central</id>
		<name>central</name>
		<url>file://d:\workspace\zhengrepo\repo</url>
		<mirrorof>*</mirrorof>
	</mirror>

</mirrors>
  
  

  
<!--  profiles
     | this is a list of profiles which can be activated in a variety of ways, and which can modify
     | the build process. profiles provided in the settings.xml are intended to provide local machine-
     | specific paths and repository locations which allow the build to work in the local environment.
     |
     | for example, if you have an integration testing plugin - like cactus - that needs to know where
     | your tomcat instance is installed, you can provide a variable here such that the variable is
     | dereferenced during the build process to configure the cactus plugin.
     |
     | as noted above, profiles can be activated in a variety of ways. one way - the activeprofiles
     | section of this document (settings.xml) - will be discussed later. another way essentially
     | relies on the detection of a system property, either matching a particular value for the property,
     | or merely testing its existence. profiles can also be activated by jdk version prefix, where a
     | value of '1.4' might activate a profile when the build is executed on a jdk version of '1.4.2_07'.
     | finally, the list of active profiles can be specified directly from the command line.
     |
     | note: for profiles defined in the settings.xml, you are restricted to specifying only artifact
     |       repositories, plugin repositories, and free-form properties to be used as configuration
     |       variables for plugins in the pom.
     |
     | -->
	 
<!--  profile
         | specifies a set of introductions to the build process, to be activated using one or more of the
         | mechanisms described above. for inheritance purposes, and to activate profiles via <activatedprofiles/>
         | or the command line, profiles have to have an id that is unique.
         |
         | an encouraged best practice for profile identification is to use a consistent naming convention
         | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
         | this will make it more intuitive to understand what the set of introduced profiles is attempting
         | to accomplish, particularly when you only have a list of profile id's for debug.
         |
         | this profile example uses the jdk version to trigger activation, and provides a jdk-specific repo.
        <profile>
          <id>jdk-1.4</id>

          <activation>
            <jdk>1.4</jdk>
          </activation>

          <repositories>
            <repository>
              <id>jdk14</id>
              <name>repository for jdk 1.4 builds</name>
              <url>http://www.myhost.com/maven/jdk14</url>
              <layout>default</layout>
              <snapshotpolicy>always</snapshotpolicy>
            </repository>
          </repositories>
        </profile>
         -->
<!-- 
         | here is another profile, activated by the system property 'target-env' with a value of 'dev',
         | which provides a specific path to the tomcat instance. to use this, your plugin configuration
         | might hypothetically look like:
         |
         | ...
         | <plugin>
         |   <groupid>org.myco.myplugins</groupid>
         |   <artifactid>myplugin</artifactid>
         |
         |   <configuration>
         |     <tomcatlocation>${tomcatpath}</tomcatlocation>
         |   </configuration>
         | </plugin>
         | ...
         |
         | note: if you just wanted to inject this configuration whenever someone set 'target-env' to
         |       anything, you could just leave off the <value/> inside the activation-property.
         |
        <profile>
          <id>env-dev</id>

          <activation>
            <property>
              <name>target-env</name>
              <value>dev</value>
            </property>
          </activation>

          <properties>
            <tomcatpath>/path/to/tomcat/instance</tomcatpath>
          </properties>
        </profile>
         -->
<!--  activeprofiles
     | list of profiles that are active for all builds.
     |
    <activeprofiles>
      <activeprofile>alwaysactiveprofile</activeprofile>
      <activeprofile>anotheralwaysactiveprofile</activeprofile>
    </activeprofiles>
     -->
</settings>

到此这篇关于maven 离线模式打包的实现示例的文章就介绍到这了,更多相关maven 离线模式打包内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com