Java构建基础知识

Java修改dependencies

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.8.6</version>
</dependency>

java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cn.idea360;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
* @author cuishiying
* @date 2022-11-03
*/
public class MavenModel {

public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
List<Dependency> dependencies = model.getDependencies();
Dependency addDependency = new Dependency();
addDependency.setGroupId("cn.idea360");
addDependency.setArtifactId("log-kafka-spring-boot-starter");
addDependency.setVersion("2.0.2");
dependencies.add(addDependency);

model.setDependencies(dependencies);
MavenXpp3Writer mavenXpp3Writer = new MavenXpp3Writer();
mavenXpp3Writer.write(new FileWriter("pom.xml"),model);
}
}

Java执行maven指令

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.0.1</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.idea360;

import org.apache.maven.shared.invoker.*;

import java.io.File;
import java.util.Arrays;

/**
* java执行maven命令: mvn clean package
*
* @author cuishiying
* @date 2022-11-03
*/
public class MavenExecutor {

public static void main(String[] args) {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("pom.xml"));
request.setGoals(Arrays.asList("clean", "package"));

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv().get("MAVEN_HOME")));

// invoker.setLogger(new PrintStreamLogger(System.err, InvokerLogger.ERROR) { });
// invoker.setOutputHandler(System.out::println);

try {
invoker.execute(request);
} catch (MavenInvocationException e) {
e.printStackTrace();
}

try {
if (invoker.execute(request).getExitCode() == 0) {
System.out.println("success");
} else {
System.err.println("error");
}
} catch (MavenInvocationException e) {
e.printStackTrace();
}
}
}

将远程仓库拉取到本地仓库

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
</dependency>

shell

1
mvn org.apache.maven.plugins:maven-dependency-plugin:3.3.0:get -D'repoUrl'=https://mvnrepository.com -D'artifact'=cn.idea360:log-kafka-spring-boot-starter:2.0.2

将jar包下载到指定目录

1
mvn dependency:copy-dependencies -DoutputDirectory=./libs

远程仓库地址即配置在pom.xml中的仓库地址

自定义META-INF/MANIFEST.MF

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifestEntries>     
<Build-Version>${maven.build.timestamp}</Build-Version>
</manifestEntries>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>

manifests读取修改

1
2
3
4
5
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-manifests</artifactId>
<version>0.7.5</version>
</dependency>
1
2
Manifests.inject("ota-site", "idea360.cn");
log.info("ota-site: {}", Manifests.read("ota-site"));

jar包基础信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private static void checkManifest() throws Exception {
log.info("项目根路径: {}", System.getProperty("user.dir"));

String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
log.info("classes-path1: {}", URLDecoder.decode(path, "utf-8"));

String path2 = ResourceUtils.getURL("classpath:").getPath();
log.info("classes-path2: {}", path2);

if (path.startsWith("file")) {
path = path.substring(5).replace("!/BOOT-INF/classes!/", "");
}
JarFile jarFile = new JarFile(path);
Manifest manifest = jarFile.getManifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.forEach((key, value) -> log.info("manifest: [{}]", key + ":" + value));
mainAttributes.put(new Attributes.Name("author"), "cuishiying");

Manifests.inject("ota-site", "idea360.cn");
log.info("ota-site: {}", Manifests.read("ota-site"));

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "META-INF/MANIFEST.MF");
for (Resource resource : resources) {
URL manifestUrl = resource.getURL();
if ("jar".equals(manifestUrl.getProtocol())
&& manifestUrl.toString().endsWith("!/META-INF/MANIFEST.MF")
&& !manifestUrl.toString().contains("/BOOT-INF/lib/")) {
log.info("MANIFEST.MF: {}", manifestUrl);
}
}

Properties properties = new Properties();
properties.load(ClassUtils.getDefaultClassLoader().getResourceAsStream("xxx.properties"));
log.info("自定义配置文件: {}", properties);

}

参考