您的位置:首页 > 其它

Maven项目的目录结构

2017-06-05 19:54 176 查看
Maven项目的目录结构
先来看看Maven的功能下面是来自于百度百科:Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,所以常常用两三行 Maven 构建脚本就可以构建简单的项目。由于 Maven 的面向项目的方法,许多 Apache Jakarta 项目发文时使用 Maven,而且公司项目采用 Maven
的比例在持续增长。
首先构建一个Maven项目,网上有很多介绍,就不重复了。整个Maven项目里很重要的就是这个pom文件。pom就是用来专门管理项目中用到的各种资源,包括jar包,jdbc驱动等,只要在pom中写下如下格式的xml,就能够自动为你下载部署该开发包(这是我的项目中的pom文件,也是在网上找的):

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.myapp.lxiao</groupId>
  5   <artifactId>MavenSSH</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>MavenSSH Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <dependencies>
 11       <!-- 添加mysql jdbc驱动 -->
 12       <dependency>
 13         <groupId>mysql</groupId>
 14         <artifactId>mysql-connector-java</artifactId>
 15         <version>5.1.34</version>
 16     </dependency>
 17     <dependency>
 18       <groupId>junit</groupId>
 19       <artifactId>junit</artifactId>
 20       <version>3.8.1</version>
 21       <scope>test</scope>
 22     </dependency>
 23     <!-- struts2核心包 -->
 24     <dependency>
 25         <groupId>org.apache.struts</groupId>
 26         <artifactId>struts2-core</artifactId>
 27         <version>2.3.1.2</version>
 28     </dependency>
 29     <!-- structs2与spring整合  -->
 30     <dependency>
 31         <groupId>org.apache.struts</groupId> 
 32            <artifactId>struts2-spring-plugin</artifactId> 
 33         <version>2.3.1.2</version> 
 34     </dependency>
 35     <!-- 在 Struts2中要使用 Ajax获得Json数据。要使用Ajax必须引用此Jar -->
 36     <dependency> 
 37         <groupId>org.apache.struts</groupId> 
 38         <artifactId>struts2-json-plugin</artifactId> 
 39         <version>2.3.1.2</version> 
 40     </dependency>
 41     <!-- Hibernate核心包 -->
 42     <dependency> 
 43         <groupId>org.hibernate</groupId> 
 44         <artifactId>hibernate-core</artifactId> 
 45         <version>3.6.10.Final</version> 
 46     </dependency>
 47     <!-- spring3可选的依赖注入,不可缺少 -->
 48     <dependency> 
 49         <groupId>org.aspectj</groupId> 
 50         <artifactId>aspectjweaver</artifactId> 
 51         <version>1.7.3</version> 
 52     </dependency>
 53     <!-- 扩展Java类与实现Java接口 -->
 54     <dependency> 
 55         <groupId>cglib</groupId> 
 56         <artifactId>cglib</artifactId> 
 57         <version>2.2</version> 
 58     </dependency>
 59     <!-- 运用Log4j必须用到这个包 -->
 60     <dependency> 
 61         <groupId>org.slf4j</groupId> 
 62         <artifactId>slf4j-api</artifactId> 
 63         <version>1.7.5</version> 
 64         <scope>compile</scope> 
 65     </dependency>
 66     <!-- Spring包 -->
 67     <!-- Spring核心包 -->
 68     <dependency>
 69         <groupId>org.springframework</groupId>
 70         <artifactId>spring</artifactId>
 71         <version>2.5.6</version>
 72         <type>jar</type> 
 73     </dependency>
 74     <!-- Spring在WEB上的MVC框架上加上这个包 -->
 75     <dependency> 
 76         <groupId>org.springframework</groupId> 
 77         <artifactId>spring-webmvc</artifactId> 
 78         <version>3.2.3.RELEASE</version> 
 79         <type>jar</type> 
 80         <scope>compile</scope> 
 81     </dependency>
 82     <!-- log4j日志包 -->
 83     <dependency> 
 84         <groupId>log4j</groupId> 
 85         <artifactId>log4j</artifactId> 
 86         <version>1.2.16</version> 
 87         <scope>compile</scope> 
 88     </dependency>
 89     <!-- jsp接口 -->
 90     <dependency> 
 91         <groupId>javax.servlet.jsp</groupId> 
 92         <artifactId>jsp-api</artifactId> 
 93         <version>2.1</version> 
 94         <scope>provided</scope> 
 95     </dependency>
 96     <!-- 连接池 -->
 97     <dependency> 
 98         <groupId>c3p0</groupId> 
 99         <artifactId>c3p0</artifactId> 
100         <version>0.9.1.2</version> 
101     </dependency>
102     <!-- servlet接口 -->
103     <dependency> 
104         <groupId>javax.servlet</groupId> 
105         <artifactId>servlet-api</artifactId> 
106         <version>2.5</version> 
107         <scope>provided</scope> 
108     </dependency>
109     <!-- Mysql数据库JDBC连接包 -->
110     <dependency> 
111         <groupId>mysql</groupId> 
112         <artifactId>mysql-connector-java</artifactId> 
113         <version>5.1.26</version> 
114         <scope>compile</scope> 
115     </dependency>
116   </dependencies>
117   <build>
118     <finalName>MavenSSH</finalName>
119   </build>yg 
120 </project>

然后我们来看一下Maven项目的目录结构,能够知道不同程序,文件都放到哪些文件夹下。

src/main/javaapplication library sources - java源代码文件
src/main/resourcesapplication library resources - 资源库,会自动复制到classes文件夹下
src/main/filtersresources filter files - 资源过滤文件
src/main/assemblyassembly descriptor - 组件的描述配置,如何打包
src/main/configconfiguration files - 配置文件
src/main/webappweb application sources - web应用的目录,WEB-INF,js,css等
src/main/bin脚本库
src/test/java单元测试java源代码文件
src/test/resources测试需要的资源库
src/test/filters测试资源过滤库
src/site一些文档
target/存放项目构建后的文件和目录,jar包,war包,编译的class文件等;Maven构建时生成的
pom.xml工程描述文件
LICENSE.txtlicense
README.txtread me
知道了maven的目录结构,我们就可以在各自的目录下创建对应的文件了。

eclipse maven 项目 出现红色叹号 解决方法
eclipse maven 项目 出现红色叹号 解决办法
因为一些maven管理的jar没能正确下载。
可以通过在eclipse中查看相应项目的build path - Configure Build Path - Libraries - Maven Dependencies,一定有些有问题的jar。
对这些有问题的jar,是因为当前的maven仓库无法找到,所以可以通过如下方法添加能找到相应jar的maven仓库:

1.在google中直接搜索相应jar,比如:jmxri-1.2.1.jar

2.在搜索结果中找到有这个jar的maven仓库,并加入到setting.xml中。

3.重新对项目进行 右键 - maven - update dependencies。
重复上述3步知道全部问题jar都正确下载。

 
另外,大象说也可以手动下载jar并手工添加到maven本地仓库,maven我不太懂,就没尝试这样干:
  大象说:先看看新配置的远程仓库是否有效,也就是是否尝试从远程仓库更新软件包,如果有效说明远程仓库也没有,这样的话,你可以用网上把你要的jar包下载下来,然后使用类似这个格式的命令mvn install:install-file -DgroupId=pinyin4j -DartifactId=pinyin4j -Dpackaging=jar -Dversion=2.5.0 -Dfile=pinyin4j-2.5.0.jar -DgeneratePom=true
可以把jar安装到本地仓库

Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0解决
maven构建项目的时候遇到这个错误:
一、直接原因
制定路径下确实没有sqljdbc4.jar文件。

二、根本原因
微软不允许以maven的方式直接下载该文件。

三、解决办法
3.1 手动下载sqljdbc4.jar,地址:http://www.microsoft.com/en-us/download/details.aspx?id=11774,将下载好的jar包放在此jar包所在maven仓库的目录(D:\maven-3.2.3\dependcies\com\microsoft\sqlserver\sqljdbc4\4.0)
3.2 执行cmd命令行并进入此jar包所在maven仓库的目录
3.3 执行以下命令
mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar

四、测试
进入maven仓库,发现sqljdbc4.jar已经安装。
执行相关项目的maven,编译通过。

给项目添加其它的远程仓库

要添加其它的远程仓库,需要在maven的conf目录下的setting.xml里面添加下面配置:

在<profiles> 节点下添加(里面的url地址就是仓库的地址,根据自己的情况替换就好了):

Xml代码  


<profile>  

      <id>dev</id>  

      <repositories>    

        <repository>    

          <id>company</id>    

          <name>company</name>    

          <url>http://192.168.2.202:8081/nexus/content/repositories/releases/</url>    

          <releases>    

            <enabled>true</enabled>    

          </releases>    

          <snapshots>    

            <enabled>false</enabled>    

          </snapshots>    

        </repository>    

      </repositories>    

      <pluginRepositories>    

        <pluginRepository>    

          <id>company</id>    

          <name>company</name>    

          <url>http://192.168.2.202:8081/nexus/content/repositories/releases/</url>    

          <releases>    

            <enabled>true</enabled>    

          </releases>    

          <snapshots>    

            <enabled>false</enabled>    

          </snapshots>        

        </pluginRepository>    

      </pluginRepositories>       

    </profile>  

同时在setting.xml最后</settings>之前加上下面的(这里的dev就是上面repository的id):

Xml代码  


<activeProfiles>  

    <activeProfile>dev</activeProfile>  

  </activeProfiles>  

这样就可以从其它的仓库下载了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: