Eclipse创建Maven项目web.xml文件、JDK版本问题解决

Eclipse创建Maven项目web.xml文件版本问题解决

Eclipse创建Maven项目修改默认index.jsp文件

Eclipse创建Maven项目修改默认pom.xml文件

Eclipse创建Maven项目修改JDK默认版本

================================

©Copyright 蕃薯耀 2019年8月6日

http://fanshuyao.iteye.com/

 

一、问题描述

Eclipse创建Maven项目时,创建出来的index.jsp页面和web.xml文件版本不是我们想要的。而且使用的jdk版本还是1.5的,如果解决这些问题,快速创建合适的版本呢?

 

二、解决方案

1、解决jdk版本是1.5的问题

方法一:修改Maven的settings.xml文件配置,在profiles(这个是有s的复数)结点增加如下配置:

<profile>
  <id>jdk-1.8</id>    
  <activation>    
     <activeByDefault>true</activeByDefault>    
     <jdk>1.8</jdk>    
  </activation>    
  <properties>    
    <maven.compiler.source>1.8</maven.compiler.source>    
    <maven.compiler.target>1.8</maven.compiler.target>    
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
  </properties>
</profile>

版本设置按照自己的需求修改,上面为JDK1.8 

完整配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<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>D:\0soft\repository</localRepository>
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
  </servers>
  <mirrors>
	 <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>
  <profiles>
	<profile>
		<id>jdk-1.8</id>    
		<activation>    
			<activeByDefault>true</activeByDefault>    
			<jdk>1.8</jdk>    
		</activation>    
		<properties>    
			<maven.compiler.source>1.8</maven.compiler.source>    
			<maven.compiler.target>1.8</maven.compiler.target>    
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
		</properties>
	</profile>

  </profiles>
</settings>

方法二:修改创建maven项目时的pom.xml文件,增加plugins配置项

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <encoding>UTF-8</encoding>
      <file>
        <name>.settings/org.eclipse.core.resources.prefs</name>
        <content>  
          <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=UTF-8${line.separator}]]>
        </content>
      </file>
    </configuration>
  </plugin>
</plugins>

 修改后保存,然后项目右键,选择Maven>>Update Project

完整代码如下:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lqy</groupId>
  <artifactId>myShiro4</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>myShiro4 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

	</dependencies>
  
  <build>
    <finalName>myShiro4</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
				<encoding>UTF-8</encoding>
				<file>
					<name>.settings/org.eclipse.core.resources.prefs</name>
					<content>  
	               <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=UTF-8${line.separator}]]>
					</content>
				</file>
			</configuration>
		</plugin>
	</plugins>
  </build>

</project>

2、解决index.jsp、web.xml文件版本问题

找到自己配置的Maven仓库下载的位置:

我的是

D:\0soft\repository\

在该目录下,进入到如下目录:

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\

 看到有个1.0的文件夹(可能有多个版本,我的只有1.0,和你创建Maven项目时选择的版本对应)

进入目录:

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0

 看到有一个Jar文件:

maven-archetype-webapp-1.0.jar

需要修改这个文件。注意:修改前请备份,请备份,请备份!

将这个Jar文件解压。

进入到archetype-resources目录:

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0\maven-archetype-webapp-1.0\archetype-resources

 首先看到的是一个pom.xml文件,我们可以修改这个文件,默认创建maven项目时就是用这个文件生成的。

修改成这样:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>${groupId}</groupId>
  <artifactId>${artifactId}</artifactId>
  <packaging>war</packaging>
  <version>${version}</version>
  <name>${artifactId} Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
  
  <build>
    <finalName>${artifactId}</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
				<encoding>UTF-8</encoding>
				<file>
					<name>.settings/org.eclipse.core.resources.prefs</name>
					<content>  
	               <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=UTF-8${line.separator}]]>
					</content>
				</file>
			</configuration>
		</plugin>
	</plugins>
  </build>

</project>

以后每个项目创建时都是这样,不用修改,也解决了第一个问题jdk版本过低的问题。 

接着,继续进入到(src\main\webapp)目录

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0\maven-archetype-webapp-1.0\archetype-resources\src\main\webapp

看到有index.jsp文件,修改成:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>标题</title>
</head>
<body>

</body>
</html>

进入到WEB-INF目录 

修改web.xml文件:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">

<web-app>
	<display-name>Archetype Created Web Application</display-name>
</web-app>

最后,退到解压后的目录:

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0\maven-archetype-webapp-1.0

 将maven-archetype-webapp-1.0目录下的2个文件夹打包成jar包即可,复制到目录

D:\0soft\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0

下即可。 

 最后的最后,送上修改好的Jar包,解压出Jar包替换就可以。记得,先备份。

下载见附件:maven-archetype-webapp-1.0.zip

================================

©Copyright 蕃薯耀 2019年8月6日

http://fanshuyao.iteye.com/

猜你喜欢

转载自www.iteye.com/blog/fanshuyao-2443263