使用IDEA+MAVEN管理项目依赖

本文介绍如何使用IntelliJ IDEA与Maven快速搭建Java项目,并实现依赖管理和自动化构建。通过配置pom.xml文件引入所需依赖,创建Ant任务运行项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java jar文件太多了,管理起来很繁琐,是在每个项目下面放一个lib文件,还是所有项目公用一个lib呢?都麻烦

 IDEA+MAVEN 试试看吧,

下载maven2

在IDEA项目根文件夹新建一pom.xml:

<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>get_train_data</groupId>
    <artifactId>get_train_data</artifactId>
    <packaging>jar</packaging> 
    <version>1.0</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testSourceDirectory>test</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>src/main/resources</directory>
            </testResource>
        </testResources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>runjar</id>
                        <phase>install</phase>
                        <configuration>
                            <tasks>
                                <property name="compile-classpath" refid="maven.compile.classpath"/>
                                <property name="runtime-classpath" refid="maven.runtime.classpath"/>
                                <ant target="runjar" inheritRefs="true"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.com/maven2</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.0.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.3-603.jdbc3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hibernate</groupId>
            <artifactId>hibernate3</artifactId>
            <version>3.2.3.GA</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hibernate-annotations</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.0.GA</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hibernate-commons-annotations</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.0.0.GA</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hibernate-entitymanager</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.3.1.GA</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>hibernate-entitymanager</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.1_3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>  commons-collections</groupId>
            <artifactId>  commons-collections</artifactId>
            <version>3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId> asm</groupId>
            <artifactId> asm</artifactId>
            <version>1.5.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.6</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

</project>

 

建立一个ant 的 build.xml文件(我需要运行我的主类GetTrainData):

<project name="get_train_data_antrun" basedir="." default="runjar">
    <path id="classpath">
        <path path="${runtime-classpath}"/>
    </path>
    <target name="runjar">
        <java classname="com.zeeeitch.GetTrainData" fork="true" maxmemory="512m">
            <classpath>
                <path refid="classpath"/>
                <path location="get_train_data-1.0.jar"/>
            </classpath>
        </java>
    </target>
</project>

 点击刷新的图标,IDEA project的dispendency自动就改了,原先设置被覆盖了

点击install吧,直接就运行了。

说明一点,maven对文件夹结构的灵活性没有IDEA好,所以只有让IDEA稍稍修改一下文件夹结构,

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值