![](/img/trans.png)
[英]Why does 'Build Project' fail whereas Run (Maven configuration) succeed?
[英]Why does the setup of a simple project with Maven and Hibernate fail?
這是一個簡單的項目,包括進行 MySQL 查詢並顯示它。 我使用 NetBeans。 hibernate.xml 和 client.xml 文件由 NetBeans(Hibernate Configuration Wizard 和 Hibernate Mapping Wizard)創建。 我真的不知道問題是什么。 1)POM文件:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>alsoMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
2) 主類:
public static void main(String[] args) {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // obtiene los valores de hibernate.cfg.xml
.build();
try {
SessionFactory sessionFactory = new MetadataSources(registry)
.buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Cliente c = (Cliente) session.load(Cliente.class, 1);
System.out.println(c);
session.getTransaction().commit();
session.close();
sessionFactory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3) 例外:
------------------------------------------------------------------------
Building alsoMaven 1.0-SNAPSHOT
------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.1.1.Final/jboss-transaction-api_1.2_spec-1.1.1.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.24.0-GA/javassist-3.24.0-GA.jar
Downloading: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.1.0.Final/hibernate-commons-annotations-5.1.0.Final.jar
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 3.184s
Finished at: Sun Mar 08 21:10:54 GMT-03:00 2020
Final Memory: 6M/16M
------------------------------------------------------------------------
Failed to execute goal on project alsoMaven: Could not resolve dependencies for project com.mycompany:alsoMaven:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.jboss.logging:jboss-logging:jar:3.3.2.Final, org.javassist:javassist:jar:3.24.0-GA, org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final, org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final: Could not transfer artifact org.jboss.logging:jboss-logging:jar:3.3.2.Final from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar. Return code is: 501 , ReasonPhrase:HTTPS Required. -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
謝謝!
從 2020 年 1 月 15 日開始,中央 Maven 存儲庫不支持通過純 HTTP 進行的不安全通信。 您必須調整本地配置。
考慮到這一點,我們在錯誤消息中找到了一個重要的細節:
返回碼為:501,ReasonPhrase: HTTPS Required 。
正如我們在輸出中看到的,您仍然使用普通的、未加密的連接來查詢/檢索來自 Maven Central 的人工制品:
下載: http ://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.1.1.Final/jboss-transaction-api_1.2_spec-1.1。 1.Final.jar
...
確保您的本地settings.xml僅包含其中包含 Maven 中央存儲庫的 http的條目。 因此,條目http://repo.maven.apache.org/maven2/
必須更改為https://repo.maven.apache.org/maven2/
。
一個最小的工作配置如下:
<profile>
<id>maven-https</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
希望能幫助到你。
您可能需要在settings.xml 中添加代理,如下所示
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>https</protocol>
<username>abc</</username>
<password>abc123</password>
<host>webproxy.company.com</host>
<port>8081</port>
<!-- <nonProxyHosts>local.net|some.host.com</nonProxyHosts> -->
</proxy>
</proxies>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.