当前位置: 代码迷 >> Web前端 >> maven jdk版本 编译有关问题
  详细解决方案

maven jdk版本 编译有关问题

热度:1386   发布时间:2013-03-12 11:19:35.0
maven jdk版本 编译问题
  1. 模块继承方式
    xxx-aggregator-->父工程 ,同时承担聚合模块和父模块的作用,没有实际代码和资源文件
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <struts2.springPlatformVersion>3.0.5.RELEASE</struts2.springPlatformVersion>
      </properties>
      
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.xxx.xxx</groupId>
      <artifactId>xxx-aggregator</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <name>xxx-aggregator</name>
      <url>http://maven.apache.org</url>
      <!-- 开发者 -->
      <developers>
      	<developer>
      		<id>juforg</id>
      		<name>S J</name>
      		<roles>
      			<role>dev</role>
      		</roles>
      		<timezone>+8</timezone>
      	</developer>
      </developers>
      <!-- 待聚合模块 -->
      <modules>
        <module>../xxx-common</module>
        <module>../xxx-web</module>
      </modules>
      
      <!-- 配置部署的远程仓库   
      <distributionManagement> 
        <snapshotRepository>  
            <id>nexus-snapshots</id>  
            <name>nexus distribution snapshot repository</name>  
            <url>http://10.78.68.122:9090/nexus-2.1.1/content/repositories/snapshots/</url>  
        </snapshotRepository>  
      </distributionManagement>  -->  
      <!-- 有的人会报错,找不到tools.jar -->
        <dependencyManagement>  
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>  
                    <artifactId>tools</artifactId>  
                    <version>1.6.0</version>  
                    <scope>system</scope>  
                    <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>  
                </dependency>
                 <dependency>
    		    	<groupId>log4j</groupId>
    		    	<artifactId>log4j</artifactId>
    		    	<version>1.2.14</version>
    		    </dependency>
            </dependencies>  
        </dependencyManagement>
    	 <build>
    	 	<pluginManagement>
    		<plugins>
    		    <!-- 使用maven的编译插件 使maven能够编译1.5以上 -->
    			<plugin>
    	    		<groupId>org.apache.maven.plugins</groupId>
    	    		<artifactId>maven-compiler-plugin</artifactId>
    	    		<version>2.5.1</version>
    	    		<configuration>
    	    			<source>1.6</source>
    	  				<target>1.6</target>
    	  				<encoding>UTF-8</encoding>
    	    		</configuration>
    	    	</plugin>
    	    	<plugin>
    	    		<groupId>org.apache.maven.plugins</groupId>
    	    		<artifactId>maven-resources-plugin</artifactId>
    	    		<version>2.6</version>
    	    		<configuration>
    	  				<encoding>UTF-8</encoding>
    	    		</configuration>
    	    	</plugin>
    		</plugins>  	
    	 	</pluginManagement>
    	 </build>
    </project>
    ?
    xxx-common-->基础工程 , 公共的代码
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
      	<groupId>com.xxx.xxx</groupId>
      	<artifactId>xxx-aggregator</artifactId>
      	<version>1.0-SNAPSHOT</version>
      	<relativePath>../xxx-aggregator</relativePath>
      </parent>
    
      <modelVersion>4.0.0</modelVersion>
      <artifactId>xxx-common</artifactId>
      <packaging>jar</packaging>
    
      <name>xxx-common</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <struts2.springPlatformVersion>3.1.1.RELEASE</struts2.springPlatformVersion>
      </properties>
    
      <dependencies>
       </dependencies>
    </project>
    
    ?
    xxx-web-->web工程 放置里公共的配置文件,比如struts.xml、ssoconfig.properties等,起到聚合的作用, 即把所有的web项目,打成最终的war包
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      
      <parent>
      	<groupId>com.jiguancheng.humabot</groupId>
      	<artifactId>xxx-aggregator</artifactId>
      	<version>1.0-SNAPSHOT</version>
      	<relativePath>../xxx-aggregator</relativePath>
      </parent>
      
      <modelVersion>4.0.0</modelVersion>
      <artifactId>xxx-web</artifactId>
      <!--打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar, par-->
      <packaging>war</packaging>
      <!-- 项目除了artifactId外,可以定义别名 -->
      <name>xxx-web</name>
      <url>http://maven.apache.org</url>
      <!-- 开发者 -->
      <developers>
      	<developer>
      		<id>juforg</id>
      		<name>S J</name>
      		<roles>
      			<role>dev</role>
      		</roles>
      		<timezone>+8</timezone>
      	</developer>
      </developers>
      <properties>
          <currentVersion>${project.version}</currentVersion>
          <struts2.version>2.3.4.1</struts2.version>
          <struts2.springPlatformVersion>3.1.1.RELEASE</struts2.springPlatformVersion>
          <ognl.version>3.0.5</ognl.version>
          <asm.version>3.3</asm.version>
          <tiles.version>2.2.2</tiles.version>
          <java.home>C:\glassfish3\jdk</java.home>
          <tomcat.home>D:\tomcat\apache-tomcat-6.0.35</tomcat.home>
      </properties>  
      <!--设置仓库 
      <repositories>
      	<repository></repository>
      </repositories>-->
      <!-- 项目的问题管理系统 
      <issueManagement>
      	<system>jira</system>
      	<url>http://111.com</url>
      </issueManagement>-->
      
      <!-- 依赖 -->
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
        </dependency>
        <dependency>
        	<groupId>org.apache.struts</groupId>
        	<artifactId>struts2-core</artifactId>
        	<version>${struts2.version}</version>
        	<exclusions>
    			<exclusion>
    				<groupId>com.sun</groupId>
    				<artifactId>tools</artifactId>
    			</exclusion>
    		</exclusions>
        </dependency>
        <dependency>
        	<groupId>log4j</groupId>
        	<artifactId>log4j</artifactId>
        </dependency>
         </dependencies>
      <build>
        <finalName>xxx-online</finalName>
        <defaultGoal>compile</defaultGoal>
        <plugins>
       	    <plugin>
        	<!-- 不加此插件会报:war files include a WEB-INF/web.xml which will be ignored -->
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-war-plugin</artifactId>
    			<version>2.1.1</version>
    			<configuration>
    			<!-- http://maven.apache.org/plugins/maven-war-plugin/ -->
    			<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
    			</configuration>
    		</plugin>
        	<!-- 自动化部署插件 Cargo,部署至tomcat7 -->
        	<plugin>
        		<groupId>org.codehaus.cargo</groupId>
        		<artifactId>cargo-maven2-plugin</artifactId>
        		<version>1.2.3</version>
        		<configuration>
       				<container>
       					<containerId>tomcat6x</containerId>
       					<home>${tomcat.home}</home>
       				</container>
      				<configuration>
      					<type>existing</type>
      					<home>${tomcat.home}</home>
      					<properties>
      						<cargo.jvmargs>
      							-Xdebug
      							-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787
      						</cargo.jvmargs>
      					</properties>
      				</configuration>
        		</configuration>
        		<executions>
        			<execution>
        				<id>cargo-run</id>
        				<phase>pre-integration-test</phase>
        				<goals>
        					<goal>run</goal>
        				</goals>
        			</execution>
        		</executions>
        	</plugin>
        	<!-- 部署到jetty -->
        	<plugin>
        		<groupId>org.mortbay.jetty</groupId>
        		<artifactId>jetty-maven-plugin</artifactId>
        		<version>8.1.5.v20120716</version>
        		<configuration>
        			<scanIntervalSeconds>10</scanIntervalSeconds>
        			<webAppConfig>
        				<!-- 从jetty的jar中拷贝出webdefault.xml 修改其中的参数 ,可以修改html,js -->
        				<defaultsDescriptor>src/main/resources/jetty.xml</defaultsDescriptor>
        				<contextPath>/${project.build.finalName}</contextPath>
        			</webAppConfig>
        			<stopKey>foo</stopKey>
        			<stopPort>9999</stopPort>
    				<reload>manual</reload>
    				<properties>
    					<cargo.jvmargs>
    						-Xdebug
    						-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787
    					</cargo.jvmargs>
    				</properties>
        		</configuration>
        	</plugin>
        </plugins>
      </build>
    </project>
    
    ?
    其他工程
  2. 是修改~/.m2/repository目录下settings.xml文件,添加一个profile,内容如下:
    <profile>
      <id>jdk-1.6</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.6</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
      </properties>
    </profile>
    
    ?

以上两个方法都比在每个pom文件中配置方便

各有各的优缺点

?

?