Bagikan melalui


Membuat templat multi-proyek

Templat multi-proyek bertindak sebagai kontainer untuk dua atau beberapa proyek Visual Studio. Saat Anda membuat proyek yang didasarkan pada templat multi-proyek, setiap proyek dalam templat ditambahkan ke solusi.

Catatan

Templat yang dibahas dalam artikel ini adalah templat yang digunakan saat Anda membuat proyek di Visual Studio, bukan templat yang dotnet new dibuat.

Templat multi-proyek memiliki dua templat proyek atau lebih dan templat akar jenis ProjectGroup.

Templat multi-proyek bertingkat berbeda dari templat proyek tunggal. Mereka memiliki karakteristik unik berikut:

  • Proyek individual dalam templat multi-proyek tidak dapat ditetapkan nama saat templat digunakan untuk membuat proyek baru. Sebagai gantinya , gunakan atribut ProjectName pada elemen ProjectTemplateLink dalam file vstemplate untuk menentukan nama untuk setiap proyek.

  • Templat multi-proyek dapat berisi proyek untuk bahasa yang berbeda, tetapi seluruh templat itu sendiri hanya dapat dimasukkan ke dalam satu kategori. Tentukan kategori templat dalam elemen ProjectType dari file vstemplate .

Templat multi-proyek harus menyertakan item berikut, dikompresi ke dalam file .zip :

  • File root vstemplate untuk seluruh templat multi-proyek. File root vstemplate ini berisi metadata yang ditampilkan dalam kotak dialog tempat Anda membuat proyek baru. Ini juga menentukan tempat menemukan file vstemplate untuk proyek dalam templat. File ini harus terletak di akar file .zip .

  • Dua folder atau lebih yang berisi file yang diperlukan untuk templat proyek lengkap. Folder mencakup semua file kode untuk proyek, dan juga file vstemplate untuk proyek.

Misalnya, file .zip templat multi-proyek yang memiliki dua proyek dapat memiliki file dan direktori berikut:

  • MultiProjectTemplate.vstemplate
  • \Project1\MyTemplate.vstemplate
  • \Project1\Project1.vbproj
  • \Project1\Class.vb
  • \Project2\MyTemplate.vstemplate
  • \Project2\Project2.vbproj
  • \Project2\Class.vb

File root vstemplate untuk templat multi-proyek berbeda dari templat proyek tunggal dengan cara berikut:

  • Atribut Jenis elemen VSTemplate memiliki nilai ProjectGroup alih-alih Project. Contohnya:

    <VSTemplate Version="2.0.0" Type="ProjectGroup"
        xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    
  • Elemen TemplateContent berisi elemen ProjectCollection yang memiliki satu atau beberapa elemen ProjectTemplateLink yang menentukan jalur ke file vstemplate dari proyek yang disertakan. Contohnya:

    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink>
                Project1\MyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink>
                Project2\MyTemplate.vstemplate
            </ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
    

Tip

Jika Anda hanya ingin templat multi-proyek muncul dalam kotak dialog proyek baru dan bukan proyek individual yang dikandungnya, tandai templat dalam sebagai tersembunyi. Contohnya:

<VSTemplate Type="Project" ... >
    <TemplateData>
        ...
        <Hidden>true</Hidden>
    </TemplateData>
    ...
</VSTemplate>

Membuat templat multi-proyek dari solusi yang sudah ada

  1. Buat solusi dan tambahkan dua proyek atau lebih.

  2. Sesuaikan proyek hingga siap diekspor ke templat.

    Tip

    Jika Anda menggunakan parameter templat dan ingin merujuk ke variabel dari templat induk, awali nama parameter dengan ext_. Contohnya,$ext_safeprojectname$. Selain itu , atur atribut CopyParameters dari elemen ProjectTemplateLink ke true.

    <ProjectTemplateLink ProjectName="MyProject" CopyParameters="true">...</ProjectTemplateLink>
    
  3. Pada menu Proyek , pilih Ekspor Templat.

    Wizard Ekspor Templat terbuka.

  4. Pada halaman Pilih Jenis Templat, pilih Templat Proyek. Pilih salah satu proyek yang ingin Anda ekspor ke templat, lalu pilih Berikutnya. (Anda akan mengulangi langkah-langkah ini untuk setiap proyek dalam solusi.)

  5. Pada halaman Pilih Opsi Templat, masukkan nama dan deskripsi opsional, ikon, dan gambar pratinjau untuk templat Anda. Pilih Selesai.

    Proyek diekspor ke dalam file .zip dan ditempatkan di lokasi output yang ditentukan.

    Catatan

    Setiap proyek harus diekspor ke templat secara terpisah, jadi ulangi langkah-langkah sebelumnya untuk setiap proyek dalam solusi.

  6. Buat direktori untuk templat Anda, dengan subdirektori untuk setiap proyek.

  7. Ekstrak konten file .zip setiap proyek ke subdirektori terkait yang Anda buat.

  8. Di direktori dasar, buat file XML dengan ekstensi file .vstemplate . File ini berisi metadata untuk templat multi-proyek. Lihat contoh berikut untuk struktur file. Pastikan untuk menentukan jalur relatif ke file vstemplate setiap proyek.

  9. Pilih semua file di direktori dasar, dan dari menu klik kanan atau konteks, pilih Kirim ke>folder Terkompresi (zip).

    File dan folder dikompresi ke dalam file .zip .

  10. Salin file .zip ke direktori templat proyek pengguna. Secara default, direktori ini adalah %USERPROFILE%\Documents\Visual Studio <version>\Templates\ProjectTemplates.

  11. Di Visual Studio, pilih File>Proyek Baru>dan verifikasi bahwa templat Anda muncul.

Contoh dua proyek

Contoh ini menunjukkan file dasar akar multi-proyek vstemplate . Dalam contoh ini, templat memiliki dua proyek, Aplikasi Windows Saya dan Pustaka Kelas Saya. Atribut ProjectName pada elemen ProjectTemplateLink menentukan nama yang diberikan untuk proyek.

Tip

Jika atribut ProjectName tidak ditentukan, nama file vstemplate digunakan sebagai nama proyek.

<VSTemplate Version="2.0.0" Type="ProjectGroup"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>Multi-Project Template Sample</Name>
        <Description>An example of a multi-project template</Description>
        <Icon>Icon.ico</Icon>
        <ProjectType>VisualBasic</ProjectType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="My Windows Application">
                WindowsApp\MyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="My Class Library">
                ClassLib\MyTemplate.vstemplate
            </ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
</VSTemplate>

Contoh dengan folder solusi

Contoh ini menggunakan elemen SolutionFolder untuk membagi proyek menjadi dua grup, Kelas Matematika dan Kelas Grafis. Templat memiliki empat proyek, dua di antaranya ditempatkan di setiap folder solusi.

<VSTemplate Version="2.0.0" Type="ProjectGroup"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>Multi-Project Template Sample</Name>
        <Description>An example of a multi-project template</Description>
        <Icon>Icon.ico</Icon>
        <ProjectType>VisualBasic</ProjectType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <SolutionFolder Name="Math Classes">
                <ProjectTemplateLink ProjectName="MathClassLib1">
                    MathClassLib1\MyTemplate.vstemplate
                </ProjectTemplateLink>
                <ProjectTemplateLink ProjectName="MathClassLib2">
                    MathClassLib2\MyTemplate.vstemplate
                </ProjectTemplateLink>
            </SolutionFolder>
            <SolutionFolder Name="Graphics Classes">
                <ProjectTemplateLink ProjectName="GraphicsClassLib1">
                    GraphicsClassLib1\MyTemplate.vstemplate
                </ProjectTemplateLink>
                <ProjectTemplateLink ProjectName="GraphicsClassLib2">
                    GraphicsClassLib2\MyTemplate.vstemplate
                </ProjectTemplateLink>
            </SolutionFolder>
        </ProjectCollection>
    </TemplateContent>
</VSTemplate>

Contoh dengan referensi proyek

Contoh ini menunjukkan cara menambahkan referensi proyek ke templat multi-proyek dan pada dasarnya merupakan ekstensi dari contoh templat multi-proyek.

Dalam contoh ini solusi berisi dua proyek, MultiProject.Client dan MultiProject.Shared. Proyek MultiProject.Client mereferensikan MultiProject.Shared.

Struktur folder adalah sebagai berikut:

  • MultiProjectTemplate.vstemplate
  • \MultiProject.Client\MultiProject.Client.csproj
  • \MultiProject.Client\MyTemplate.vstemplate
  • \MultiProject.Shared\MultiProject.Shared.csproj
  • \MultiProject.Shared\MyTemplate.vstemplate

Ketika templat digunakan, bagian MultiProyek sedang diganti dengan nama proyek yang dimasukkan pengguna.

MultiProjectTemplate.vstemplate terlihat seperti berikut ini. Harap dicatat bahwa ProjectTemplateLinkmemiliki atribut CopyParameters yang diatur ke true dan bahwa atribut ProjectName menggunakan variabel templat $safeprojectname$.

<VSTemplate Version="2.0.0" Type="ProjectGroup"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
...
<TemplateContent>
    <ProjectCollection>
        <ProjectTemplateLink ProjectName="$safeprojectname$.Client" CopyParameters="true">
            MultiProject.Client\MyTemplate.vstemplate
        </ProjectTemplateLink>
        <ProjectTemplateLink ProjectName="$safeprojectname$.Shared" CopyParameters="true">
            MultiProject.Shared\MyTemplate.vstemplate
        </ProjectTemplateLink>
    </ProjectCollection>
</TemplateContent>
</VSTemplate>

MultiProject.Client\MyTemplate.vstemplate mungkin terlihat seperti berikut ini. Harap dicatat bahwa tag Proyek memiliki atribut ReplaceParameters yang diatur ke true.

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
	...
	<TemplateContent>
		<Project TargetFileName="MultiProject.Client.csproj" File="MultiProject.Client.csproj" ReplaceParameters="true">
            ...
		</Project>
	</TemplateContent>
</VSTemplate>

MultiProject.Client\MultiProject.Client.csproj mungkin terlihat seperti berikut ini. Harap dicatat bahwa atribut Include of the ProjectReference menggunakan variabel templat $ext_safeprojectname$

<Project>
    ...
	<ItemGroup>
	  <ProjectReference Include="..\$ext_safeprojectname$.Shared\$ext_safeprojectname$.Shared.csproj" />
	</ItemGroup>
</Project>