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

  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值