编程知识 cdmana.com

Idea + Maven install to run jar package

IDEA+Maven install It can run jar package

    • One 、 purpose
    • Two 、 To configure Maven Compile the plug-in
      • 1、 newly build mavan project
      • 2、 introduce maven To configure
    • 3、 ... and 、 matters needing attention

One 、 purpose

The simple and direct idea is to type it out jar Be able to directly java -jar function , Mainly want to do some small tools to run the program , Such as encryption and decryption of small programs

Actually idea and eclipse And so on programming tool itself supports the project to hit jar , But it's typed this way jar The main list of programs is usually missing , Can not run , So we think about using maven-assembly-plugin

Two 、 To configure Maven Compile the plug-in

1、 newly build mavan project

This is needless to say , For example, we want to make a small program for encryption and decryption here , for example :DESUtil Tool class

2、 introduce maven To configure

stay pom.xml Join in :

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <archive>
                        <manifest>
                        	<!--  Configure your program entry here main -->
                            <mainClass>util.DESUtils</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

packaging :

If package You have to add assembly:single, Otherwise, only one will be generated test-1.0-SNAPSHOT.jar( there test Project name ), And the runtime will report an error :jar There is no master list attribute in

The following commands need to be executed :

mvn package assembly:single 

After packing, it will be in target Create one in the directory test-jar-with-dependencies.jar file , This file contains not only the code and resources from your own project , It also contains the contents of all dependent packages . So you can go straight through java -jar To run the

If you don't want to be so troublesome , Want to be directly in idea in maven package If you pack it , Add the following configuration :

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <archive>
                        <manifest>
                        	<!--  Configure your program entry here main -->
                            <mainClass>util.DESUtils</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

among package、single That is to say, it is executing package When the packaging , perform assembly:single, So you can use it directly mvn package pack

3、 ... and 、 matters needing attention

If we maven Of settings.xml The image address configured inside is private service , It's better to change it to maven Warehouse , because maven-assembly-plugin There are a lot of cascading references jar, No change to maven Warehouse address , It's easy to report the lack of Class It's abnormal for example : A required class was missing while executing org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single: Lorg/apache/maven/shared/filtering/MavenFileFilter;

change settings.xml

    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[The blank of writing memory]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224104023984e.html

Scroll to Top