当前位置: 首页 > news >正文

Maven使用过程中遇到的问题,及解决方案

为什么80%的码农都做不了架构师?>>>   hot3.png

  1. 多模块项目的项目依赖关系,定义编译顺序
          <!--

       dmo一般定义bean

       commons里面一般定义工具类

       biz里面是业务逻辑

       provider是对外开放的接口

       main是服务的启动

       鉴于代码如此分布,依赖关系一般也是按此顺序,所以在modules中定义模块,需要根据依赖顺序安排先后

   -->

<modules>

   <module>service-outgate-dmo</module>

   <module>service-outgate-commons</module>

   <module>service-outgate-biz</module>

   <module>service-outgate-provider</module>

   <module>service-outgate-main</module>

</modules>

 

<!--

模块之间存在依赖关系,需要弄清楚模块之间的依赖顺序,编译的时候被依赖者放到modules元素中的靠前位置。

模块如果互相依赖,要注意编译顺序。

   尽量避免模块之间的循环依赖

-->

<modules>

   <module>service-video-dmo</module>

   <module>service-video-commons</module>

   <module>service-video-biz</module>

   <module>service-video-provider</module>

   <module>service-video-main</module>

</modules>

 

<!--

   maven多级项目,需要考虑同级父项目的子项目间的依赖关系,并行开发的服务,最好中间不要产生依赖。

   不同业务的请求和响应对象,在分项目的时候,不同业务之前的bean尽量不要产生依赖。子项目间的复杂

   依赖关系,后导致后期项目编译依赖过于繁琐。导致pom文件的依赖不好定义,会使之往更糟糕的方向发展。

-->

<modules>

   <module>service-user</module>

   <module>service-outgate</module>

   <module>service-video</module>

   <module>service-msg</module>

</modules>

 

以上可能出现的问题就是,编译找不到类的方法。

 

  1. 针对Nexus中依赖有pom没有jar

在Nexus中依赖有pom没有jar,一般pom里面都是有jar的地址信息,maven去下载的时候下载不下来,可能是因为网络,可能是因为jar服务提供上收费,这种情况下,一般建议本地安装jar到Nexus,不到第三方下载,将jar安装在Nexus的thirtyParty目录

  1. 依赖重复的情况

      

  1. Maven内置的变量

${basedir} 项目根目录 

${project.build.directory} 构建目录,缺省为target 

${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes 

${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version} 

${project.packaging} 打包类型,缺省为jar 

${project.xxx} 当前pom文件的任意节点的内容 

 

  1. 是选择Maven编译插件进行编译,还是使用Pom默认的,为什么要配置编译插件

 

  1. 资源文件怎么打包,怎么配置资源文件的打包

 

 

  1. Profile环境是怎么选择的

       Profile为不同的环境可以配置不同的打包要求,在profile中可以覆盖pom中的几乎所有元素。在打包运行的时候需要在命令行传参指定运行那个Profile,这个参数就是profile的id.

<profile>

   <id>local</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_local.properties</context.conf.path>

   </properties>

   <activation>

      <activeByDefault>true</activeByDefault>

   </activation>

</profile>



<profile>

   <!-- 开发环境 -->

   <id>dev</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_dev.properties</context.conf.path>

   </properties>

</profile>

<profile>

   <!-- 测试环境 -->

   <id>test</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_test.properties</context.conf.path>

   </properties>

</profile>

</profiles>


假如打包本地环境,命令为:

       mvn clean install –Plocal

此种方式,是手动激活,指定运行某种环境构建。

可以通过在profile中配置激活策略,当构建环境满足某些构建策略时,进行某个profile的构建。激活配置放在上面加粗标红的元素里。

 

Profile可以单独成立文件,引入到pom。也可以在私服的settings.xml中设置全局的profile,被所有项目共享。

  1. pluginspluginManagement

       pluginManagerment一般使用在父pom中,用来管理所有子项目需要用到的插件。在父pom中,声明插件所有的基本信息(版本号,JDK版本等等),在子pom中使用使用插件只需groupIdartifactId即可,无需配置其他的额外信息,这样可以做到所有子项目使用同样的插件配置。如果子项目,需要使用不同的参数配置,在子pom中可以自行声明。

在父pom中管理所有子项目可能需要使用到的插件,子项目不声明使用父pom中的插件,则子项目就不会用到该插件。

父pom中的配置:

<pluginManagement>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-source-plugin</artifactId>

            <version>2.1</version>

            <configuration>

                <attach>true</attach>

            </configuration>

            <executions>

                <execution>

                    <phase>compile</phase>

                    <goals>

                        <goal>jar</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

</pluginManagement>

 

子pom中的配置:

<plugins>

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-source-plugin</artifactId>

    </plugin>

</plugins>
 

pluginManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。Plugins则相反,声明的都会引入。

DependencysdependencyManager是同一样的道理。

      

  1. Maven打包,配置文件乱码问题

     要找出文件乱码的根源,就需要知道编码的流程。从编写文件到打成发布包,中间经历几次转码。

     首先编写文件,编辑器或IDE会默认为文件指定编码。

     其次文件会经过编译器编译,不过配置文件,应该不会被编译器处理。

     Copy文件到要打的包中,这个过程maven是怎么获取编码方式的,默认取机器编码,还是获取项目文件编码,或者是在pom文件中指定编码变量供其读取使用。

     Maven获取编码的顺序应该是:

如果为指定project.build.sourceEncoding编码,maven使用默认,指定则使用指定。插件也可以指定编码,如果插件指定了编码,则使用插件自身指定的编码。
<properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      </properties>
  
 <configuration>
      <encode>utf-8</encode>  
</configuration> 

 

  1. Redhat-7关闭防火墙的办法,一般端口不能访问都是防火墙的问题。

在之前的版本中关闭防火墙等服务的命令是

 

 

 

service iptables stop

/etc/init.d/iptables stop

 

 

在RHEL7中,其实没有这个服务

 

 

 

[root@rhel7 ~]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 7.0 (Maipo)

[root@rhel7 ~]# service iptables stop

Redirecting to /bin/systemctl stop  iptables.service

[root@rhel7 ~]# /etc/init.d/iptables stop

-bash: /etc/init.d/iptables: No such file or directory

 

 

原来在RHEL7开始,使用systemctl工具来管理服务程序,包括了service和chkconfig

systemctl list-unit-files|grep enabled

 

[root@rhel7 ~]# systemctl stop firewalld.service

[root@rhel7 ~]# systemctl disable firewalld.service

[root@rhel7 ~]# systemctl status firewalld.service

 

 

启动一个服务:systemctl start firewalld.service

关闭一个服务:systemctl stop firewalld.service

重启一个服务:systemctl restart firewalld.service

显示一个服务的状态:systemctl status firewalld.service

在开机时启用一个服务:systemctl enable firewalld.service

在开机时禁用一个服务:systemctl disable firewalld.service

查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?

查看已启动的服务列表:systemctl list-unit-files|grep enabled

 

  1. Maven-war-plugin的使用

正常打包war项目时,只需标示项目打包类型即可实现web项目的war打包。但是项目不知为何单独声明war-plugin,其指定的不同属性导致打包的不同类型。

<plugin>

   <groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-war-plugin</artifactId>

   <version>2.2</version>

   <configuration>

      <warName>etopmiddle-oms-web</warName>

      <!--是否将war项目打成jar包,如果设置为TRUE则将web项目打成jar,war中包含配置文件和lib,

      设置为false,则是常规war-->

      <archiveClasses>false</archiveClasses>

      <webResources>

         <resource>

            <!-- this is relative to the pom.xml directory -->

            <directory>src/main/resources</directory>

            <targetPath>WEB-INF/classes</targetPath>

            <!-- the list has a default value of ** -->

            <includes>

               <include>**</include>

            </includes>

            <filtering>true</filtering>

         </resource>

      </webResources>

   </configuration>

</plugin>

 

以上即为项目中的war-plugin的声明。其意图很明显,是将war中的class文件打包成jar,然后在class的jar作为lib引用,war中只包含配置文件。但是这样打包,会引起配置文件的乱码。避免乱码的最好方式就是不使用非英文。注释掉该配置,项目打包恢复正常。

转载于:https://my.oschina.net/773355/blog/1839396

相关文章:

  • linux磁盘管理
  • spring配置中classpath: 与classpath*:的区别
  • 虚拟机(Virtual Machine)和容器(Container)的对比
  • Linux第四章 进程
  • css选择器有哪些
  • Hbase备份
  • 前端战五渣学前端——初探Parcel急速打包
  • Android程序员搞Web之HTNL(一)
  • mysql-proxy安装过程 (转)
  • Day3LJY
  • angular 摇树优化
  • EOS 坑 右击java文件封装成Web Service不弹界面
  • ./configure、make、make install 命令
  • div浮动+盒子模型+溢出隐藏+滚动条
  • JavaScript创建对象的四种方式
  • Android Volley源码解析
  • Consul Config 使用Git做版本控制的实现
  • Cumulo 的 ClojureScript 模块已经成型
  • Javascript基础之Array数组API
  • Joomla 2.x, 3.x useful code cheatsheet
  • JS 面试题总结
  • JS学习笔记——闭包
  • maya建模与骨骼动画快速实现人工鱼
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • nodejs调试方法
  • npx命令介绍
  • React16时代,该用什么姿势写 React ?
  • springMvc学习笔记(2)
  • 创建一个Struts2项目maven 方式
  • 高程读书笔记 第六章 面向对象程序设计
  • 基于axios的vue插件,让http请求更简单
  • 排序(1):冒泡排序
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​Java并发新构件之Exchanger
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #1015 : KMP算法
  • #mysql 8.0 踩坑日记
  • ()、[]、{}、(())、[[]]命令替换
  • (八)c52学习之旅-中断实验
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (理论篇)httpmoudle和httphandler一览
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .CSS-hover 的解释
  • .java 9 找不到符号_java找不到符号
  • .jks文件(JAVA KeyStore)
  • .net 4.0发布后不能正常显示图片问题
  • .net 简单实现MD5