1. 程式人生 > >使用IDEA+MAVEN管理專案依賴

使用IDEA+MAVEN管理專案依賴

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稍稍修改一下資料夾結構,