ניהול חבילות Java

בדף הזה מוסבר איך להוסיף, לראות ולמחוק חבילות Java.

אם רוצים להשתמש ב-sbt ככלי build ל-Scala ול-Java במקום להשתמש ב-Maven או ב-Gradle, אפשר להשתמש בתוסף sbt שנוצר על ידי הקהילה. במסמך הזה לא מתואר השימוש בלקוחות Scala או ההגדרה שלהם.

לפני שמתחילים

  1. אם מאגר היעד לא קיים, צריך ליצור מאגר חדש.
  2. מוודאים שיש לכם את ההרשאות הנדרשות למאגר.
  3. מוודאים שהגדרתם אימות. אם אתם משתמשים באסימון גישה, חשוב לרענן את האסימון לפני שמתחברים למאגר.
  4. (אופציונלי) הגדרת ברירות מחדל לפקודות gcloud.

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות לניהול חבילות, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים במאגר:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

הוספת חבילות

מצבי מאגר: Standard

כדי להוסיף חבילות למאגר:

Maven

משתמשים בפקודות mvn deploy ו-mvn release כדי להוסיף חבילות למאגר.

כדי לפרוס בהצלחה פרויקט Maven שמפנה לפרויקט אב, הפרויקט צריך לכלול את ספק Wagon של Artifact Registry בקובץ של תוספי ליבה, כפי שמתואר בהוראות האימות.

משתמשים ב-mvn deploy:deploy-file כדי להעלות ארטיפקטים שנבנו מחוץ ל-Maven.

לדוגמה, הפקודה הבאה פורסת את example/external.jar ואת קובץ הפרויקט example/pom.xml שלו במאגר us-central1-maven.pkg.dev/my-project/my-repo

mvn deploy:deploy-file \
-Durl=artifactregistry://us-central1-maven.pkg.dev/my-project/my-repo \
-DpomFile=example/pom.xml -Dfile=example/external.jar

איך משלבים עם Cloud Build

Gradle

כדי לפרסם בהצלחה במאגר, קובץ build.gradle צריך לכלול קטע publications שבו מוגדרים הקבצים להעלאה.

משתמשים בפקודה gradle publish כדי להעלות חבילה למאגר.

צפייה בחבילות ובגרסאות

מצבי מאגר: רגיל, מרוחק

To view packages and package versions using the Google Cloud console or gcloud:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of the package.

gcloud

To list packages in a repository, run the following command:

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. If you configured a default location, then you can omit this flag to use the default.

To view versions of a package, run the following command:

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • PACKAGE is the ID of the package or fully qualified identifier for the package.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

עבור מאגרי מידע מרוחקים, הרשימה שמוחזרת צריכה לכלול את כל התלויות הישירות והטרנזיטיביות.

מרכיב רשימת קבצים

מצבי מאגר: רגיל, מרוחק

אפשר להציג רשימה של קבצים במאגר, קבצים בכל הגרסאות של חבילה שצוינה או קבצים בגרסה ספציפית של חבילה.

בכל הפקודות הבאות, אפשר להגדיר מספר מקסימלי של קבצים להחזרה על ידי הוספת הדגל --limit לפקודה.

כדי להציג רשימה של כל הקבצים בפרויקט, במאגר ובמיקום שמוגדרים כברירת מחדל כשמוגדרים ערכי ברירת מחדל:

gcloud artifacts files list

כדי לראות את רשימת הקבצים בפרויקט, במאגר ובמיקום שצוינו, מריצים את הפקודה:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION

כדי להציג רשימה של קבצים לכל הגרסאות של חבילה מסוימת:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE

כדי להציג רשימה של קבצים בגרסה ספציפית של חבילה:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --version=VERSION

מחליפים את הערכים הבאים:

  • LOCATION: המיקום האזורי או הרב-אזורי של המאגר.
  • PROJECT: Google Cloud מזהה הפרויקט. אם מזהה הפרויקט מכיל נקודתיים (:), כדאי לעיין במאמר בנושא פרויקטים בהיקף הדומיין.
  • REPOSITORY: השם של המאגר שבו התמונה מאוחסנת.
  • PACKAGE: שם החבילה.
  • VERSION: הגרסה של החבילה.

דוגמאות

כדאי לעיין בפרטי החבילה הבאים:

  • פרויקט: my-project
  • מאגר: my-repo
  • מיקום המאגר: us-west1
  • חבילה: my-app

הפקודה הבאה מציגה רשימה של כל הקבצים במאגר my-repo במיקום us-west1 בתוך פרויקט ברירת המחדל:

gcloud artifacts files list \
    --location=us-west1 \
    --repository=my-repo
הפקודה הבאה מציגה רשימה של קבצים בגרסה 1.0 של החבילה.

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --version=1.0

הורדת חבילות

מצבי מאגר: רגיל, מרוחק, וירטואלי

כדי להוריד ארטיפקט כחלק מה-build, צריך להגדיר את הארטיפקט כתלות.

Maven

  1. מצהירים על החבילות שרוצים להוריד בקובץ pom.xml של הפרויקט. בדוגמה הבאה מוצהרת גרסה 1.0 של החבילה artifact כתלות.

    <dependencies>
        <dependency>
            <groupId>group</groupId>
            <artifactId>artifact</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
    

    מידע נוסף על יחסי תלות ב-Maven זמין במאמרים introduction to dependencies ו-dependency reference.

  2. מבצעים Build לפרויקט Java.

    mvn compile
    

Gradle

  1. מצהירים על החבילות שרוצים להוריד בקובץ build.gradle של הפרויקט. בדוגמה הבאה מוצהרת גרסה 1.0 של החבילה artifact כתלות חיצונית בהידור.

    מידע נוסף על יחסי תלות ב-Gradle זמין במסמכי Gradle.

    dependencies {
        compile group: 'group', name: 'artifact', version: '1.0'
    }
    
  2. מבצעים Build לפרויקט Java.

    gradle build
    

במאגרי מידע רגילים, מורידים חבילה ישירות ממאגר המידע.

אם המאגר מרוחק, מורידים עותק בקובץ שמור של החבילה ושל התלות שלה. אם אין עותק במטמון, המאגר המרוחק מוריד את החבילה ממקור ה-upstream ושומר אותה במטמון לפני שהוא מציג אותה. כדי לוודא שהחבילות אוחזרו מהמקור במעלה הזרם למאגר המרוחק, אפשר לצפות ברשימה של החבילות במאגר.

במאגר וירטואלי, מערכת Artifact Registry מחפשת את החבילה המבוקשת במאגרים במעלה הזרם.

  • מאגרי מידע מרוחקים במעלה הזרם יורידו את החבילה המבוקשת וישמרו אותה במטמון אם לא קיים עותק במטמון. מאגרים וירטואליים מציגים רק חבילות מבוקשות, ולא מאחסנים אותן.
  • אם מבקשים גרסה שזמינה ביותר ממאגר אחד במעלה הזרם, Artifact Registry בוחר מאגר במעלה הזרם לשימוש על סמך הגדרות העדיפות שהוגדרו למאגר הווירטואלי.

לדוגמה, נניח שיש מאגר וירטואלי עם הגדרות העדיפות הבאות למאגרי upstream:

  • main-repo: העדיפות מוגדרת ל-100
  • secondary-repo1: העדיפות מוגדרת ל-80.
  • secondary-repo2: העדיפות מוגדרת ל-80.
  • test-repo: העדיפות מוגדרת ל-20.

ל-main-repo יש את ערך העדיפות הגבוה ביותר, ולכן המאגר הווירטואלי תמיד מחפש בו קודם.

העדיפות של secondary-repo1 ושל secondary-repo2 מוגדרת כ80. אם חבילה מבוקשת לא זמינה ב-main-repo, ‏ Artifact Registry מחפש אותה במאגרים האלה. מכיוון שלשני המאגרים יש אותו ערך עדיפות, אם הגרסה זמינה בשניהם, Artifact Registry יכול לבחור להציג חבילה מכל אחד מהם.

ל-test-repo יש את ערך העדיפות הנמוך ביותר, והוא יציג פריט מאוחסן אם הוא לא נמצא באף אחד ממאגרי המידע האחרים במעלה הזרם.

מחיקת חבילות וגרסאות

מצבי מאגר: רגיל, מרוחק

אתם יכולים למחוק חבילה ואת כל הגרסאות שלה, או למחוק גרסה ספציפית.

  • אחרי שמוחקים חבילה, אי אפשר לבטל את הפעולה.
  • עבור מאגרי מידע מרוחקים, רק העותק שבמטמון של החבילה נמחק. אין השפעה על מקור הנתונים במעלה הזרם. אם מוחקים חבילה ששמורה במטמון, Artifact Registry יוריד אותה וישמור אותה במטמון שוב בפעם הבאה שהמאגר יקבל בקשה לאותה גרסת חבילה.

Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Replace the following:

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Replace the following:

  • VERSION is the name of the version to delete.
  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

המאמרים הבאים